"Can't convert 'float' object to str implicitly"

Question:

>>> def main():
        fahrenheit = eval(input("Enter the value for F: "))
        celsius = fahrenheit - 32 * 5/9
        print("The value from Fahrenheit to Celsius is " + celsius)
>>> main()
Enter the value for F: 32
Traceback (most recent call last):  
  File "<pyshell#73>", line 1, in <module>
    main()
  File "<pyshell#72>", line 4, in main
    print("The value from Fahrenheit to Celsius is " + celsius)
TypeError: Can't convert 'float' object to str implicitly
Asked By: Shohin

||

Answers:

As the error says, you cannot covert float object to string implicitly. You have to do :

print("The value from Fahrenheit to Celsius is " + str(celsius))
Answered By: jester

floats can’t be implicitly converted into strings. You need to do it explicitly.

print("The value from Fahrenheit to Celsius is " + str(celsius))

But it’s better to use format.

print("The value from Fahrenheit to Celsius is {0}".format(celsius))
Answered By: Jayanth Koushik
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.