What's wrong with my code to convert farenheight to Celsius?

折月煮酒 提交于 2020-01-30 13:17:48

问题


#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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!