我已经搜索并找不到如何解决这个问题,或者如何解决它。任何帮助表示赞赏,谢谢。TypeError:无法连接'str'和'float'对象
我的代码如下:
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”:
请输入一个整数,其后由单个空间然后或者华氏,C A F为摄氏,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
>>>
打开字符串转换为浮动或漂浮到这取决于期望的字符串。 – 2013-03-26 03:58:42
请修复您的缩进。如果他们没有立即看到这些块,这是可以理解的,但对他人无益。 – 2013-03-26 04:00:51