2013-03-26 140 views
2

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

打開字符串轉換爲浮動或漂浮到這取決於期望的字符串。 – 2013-03-26 03:58:42

+0

請修復您的縮進。如果他們沒有立即看到這些塊,這是可以理解的,但對他人無益。 – 2013-03-26 04:00:51

回答

3

raw_input全部更換temperature[:-1]返回用戶輸入的string。你在找什麼是float。在Python中,無法將stringfloat對象一起添加。所以,你將不得不做的第一個輸入轉換爲float,以便您可以對其執行數學運算。

這是我會怎麼做:

user_input = raw_input("Prompt Text...").split(' ') 

temperature = float(user_input[0]) 
units_or_option = user_input[1] 

所以,split方法使一個列表出來什麼樣的用戶輸入,使用空格字符分割的條目。 temperature包含前面的第一件東西,鑄造成floatunits_of_option包含在數字和空格後輸入的字符。

因此,如果用戶輸入:

123.5 F 

變量的數值:

user_input = ["123.5", "F"] 
temperature = 123.5 
units_or_option = "F" 
1
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: ") 

raw_input返回一個字符串,所以你想分割字符串或將它轉換成每一步到一個浮動。

它總是最好設置爲變量的數量,而不是重複計算它的,所以你可以做第

將其轉換爲浮點數:

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: ") 
temp = float(temperature.split(' ')[0]) 

返回什麼string.split(obj)所做的是string的部分清單,將其與obj分開。例如,如果用戶輸入273 K,並因此temperature == '273 K',temperature.split(' ')通過在每個空間拆分它創建一個列表,該空間變爲['273', 'K']。但是,這些仍然是字符串,所以我們想將第一個轉換爲浮點數,所以我們做float(temperature.split(' ')[0]) # we only want to convert the first element

你改進的代碼,用temp更換temperature[:-1]unit更換temperature[-1](它總是良好的編程習慣,以創建變量,而不是計算值多次):

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 
    temp, unit = float(temperature.split(' ')[0]), temperature[-1] # multi-assignment 
    if unit == "F": 
     K = (temp + (459.67) * (5.0/9.0)) 
     print K 
     C = (temp - 32) * (5.0/9.0) 
     print C 
     R = (temperature[:-1] + 459.67) 
     print R 
    # algorithm for Celsius to Kelvin, Fahrenheit, and Rankine 
    elif unit == "C": 
     K = (temp + 273.15) 
     print K 
     F = (temp * (9.0/5.0) + 32) 
     print F 
     R = (temp + 273.15) * (9.0/5.0) 
     print R 
    # algorithm for Kelvin to Celsius, Fahrenheit, and Rankine 
    elif unit == "K": 
     C = (temp - 273.15) 
     print C 
     F = (temp * (9.0/5.0) - 459.67) 
     print F 
     R = (temp * (9.0/5.0)) 
     print R 
    # algorith for Rankine to Fahrenheit, Celsius, and Kelvin 
    elif temperature[-1] == "R": 
     F = (temp - 459.67) 
     print F 
     C = (temp - 491.67) * (5.0/9.0) 
     print C 
     K = (temp * (5.0/9.0)) 
     print K 
main() 
2

你需要的只是2點的變化:
1:剛raw_input
添加temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1]temperature
例如:

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: ") 
temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1] 
##. . . . your code 


第二:
temperature[0]



您的最終代碼

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 
    temperature=float(temperature.split(' ')[0]),temperature.split(' ')[1] 
    if temperature[-1] == "F": 
     K = (temperature[0] + (459.67) * (5.0/9.0)) 
     print K 
     C = (temperature[0] - 32) * (5.0/9.0) 
     print C 
     R = (temperature[0] + 459.67) 
     print R 
    # algorithm for Celsius to Kelvin, Fahrenheit, and Rankine 
    elif temperature[-1] == "C": 
     K = (temperature[0] + 273.15) 
     print K 
     F = (temperature[0] * (9.0/5.0) + 32) 
     print F 
     R = (temperature[0] + 273.15) * (9.0/5.0) 
     print R 
    # algorithm for Kelvin to Celsius, Fahrenheit, and Rankine 
    elif temperature[-1] == "K": 
     C = (temperature[0] - 273.15) 
     print C 
     F = (temperature[0] * (9.0/5.0) - 459.67) 
     print F 
     R = (temperature[0] * (9.0/5.0)) 
     print R 
    # algorith for Rankine to Fahrenheit, Celsius, and Kelvin 
    elif temperature[-1] == "R": 
     F = (temperature[0] - 459.67) 
     print F 
     C = (temperature[0] - 491.67) * (5.0/9.0) 
     print C 
     K = (temperature[0] * (5.0/9.0)) 
     print K 
main() 
+0

我可以知道爲什麼投票下來嗎? – namit 2013-03-26 04:09:57

相關問題