问题
#Programme to covert Farenheight to Celcius
F=input("Enter Value:")
F=((F-32)/9)*5
print("The temperature is ",F,"Degrees Celcius")
When i try to run it it says TypeError: unsupported operand type(s) for -: 'str' and 'int'
回答1:
Type cast you input to an int
F=int(input("Enter Value:"))
And force the division to return a float. Add this at the very top
from __future__ import division
回答2:
`F=((float(F)-32)/9)*5`
Or
F=float(input('Enter value'))
[...]`
Input return strings, you have to change them into int or float.
回答3:
You need to work with floating point numbers:
#Programme to covert Farenheight to Celcius
F = float(input("Enter Value:"))
F = ((F-32.0) / 9.0) * 5.0
print("The temperature is {:.1f} Degrees Celcius".format(F))
Giving you:
Enter Value:33
The temperature is 0.6 Degrees Celcius
The {:.1f}
tells it to format F
the floating point result as a string to one decimal place.
来源:https://stackoverflow.com/questions/43661130/whats-wrong-with-my-code-to-convert-farenheight-to-celsius