我已经搜索过,无法弄清楚这个问题是如何出现的,或者如何解决它。任何帮助表示赞赏,谢谢。
我的代码如下:
def main():
temperature = raw_input("Please enter an integer followed by a single space then either a F for Fahrenheit, C for Celsius, K for Kelvin, or R for Rankine: ")
# algorithm for Farenheit to Kelvin, Celsius, and Rankine
if temperature[-1] == "F":
K = (temperature[:-1] + (459.67) * (5.0/9.0))
print K
C = (temperature[:-1] - 32) * (5.0/9.0)
print C
R = (temperature[:-1] + 459.67)
print R
# algorithm for Celsius to Kelvin, Fahrenheit, and Rankine
elif temperature[-1] == "C":
K = (temperature[:-1] + 273.15)
print K
F = (temperature[:-1] * (9.0/5.0) + 32)
print F
R = (temperature[:-1] + 273.15) * (9.0/5.0)
print R
# algorithm for Kelvin to Celsius, Fahrenheit, and Rankine
elif temperature[-1] == "K":
C = (temperature[:-1] - 273.15)
print C
F = (temperature[:-1] * (9.0/5.0) - 459.67)
print F
R = (temperature[:-1] * (9.0/5.0))
print R
# algorith for Rankine to Fahrenheit, Celsius, and Kelvin
elif temperature[-1] == "R":
F = (temperature[:-1] - 459.67)
print F
C = (temperature[:-1] - 491.67) * (5.0/9.0)
print C
K = (temperature[:-1] * (5.0/9.0))
print K
main()
以及输入“50 F”后的错误消息:
请输入一个整数,后跟一个空格,然后输入 F 代表华氏温度、C 代表摄氏温度、K 代表开尔文或 R 代表兰金:50 F
Traceback (most recent call last):
File "/Users/Minotza/Documents/multi-unit_temperature_converter.py", line 36, in <module>
main()
File "/Users/Minotza/Documents/multi-unit_temperature_converter.py", line 5, in main
K = (temperature[:-1] + (459.67) * (5.0/9.0))
TypeError: cannot concatenate 'str' and 'float' objects
>>>