-2

I am trying to programming a code that successfully converts Fahrenheit to Celsius but I keep getting an error which states "TypeError: cannot concatenate 'str' and 'float' objects"

Code Given Below:

a = float(raw_input("What is the temperature in fahrenheit ")) 
b = (a-32)/1.8 
c = "Your temperature is " + b + " degrees celsius."
JayPee
  • 3
  • 1
  • 5

2 Answers2

1

Python does not know what to do when it sees string + float + string (most other languages do not either).

In order to get the result you're looking for, you need to convert b into a string first. There are a few ways to do this, depending on how many decimal places you want to retain for b once you convert it.

Here's a useful stack overflow question that should help you on your way :)

Converting a float to a string without rounding it

cptwonton
  • 500
  • 3
  • 16
0

You need to change b from float to string:

c = "Your temperature is " + str(b) + " degrees celsius."

Or, you may use substitution to print your output:

c = "Your temperature is %s degrees celsius." % b
AnythingIsFine
  • 1,777
  • 13
  • 11