我已經搜索並找不到如何解決這個問題,或者如何解決它。任何幫助表示讚賞,謝謝。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