2016-01-21 113 views
-2

我想找到解決這個問題已經2個小時了,我不知道該怎麼輸入這樣的功能?溫度轉換蟒蛇功能

num = input ('Which Degree Temperature would you like to convert? ') 
corf = raw_input ('Is the temperature Fahrenheit Degree or is it Celsius?(Type "f" for Fahrenheit or "c" for Celsius)') 
def max(num,corf): 
    if corf == 1 : 
     return num 
     Celsius = (num - 32) * 5.0/9.0 
     print str(num) + " Fahrenheit is equal to " + str(Celsius)+ " Celsius or " + ('%0.1f degrees Celsius ' %(Celsius)) + 'to one decimal place ' 
    elif corf == 2 : 
     return num 
     Fahrenheit = (num * 9.0/5.0) + 32 
     print str(num) + " Celsius is equal to " + str(Fahrenheit)+ " Fahrenheit or " + ('%0.1f degrees Celsius ' %(Celsius)) + 'to one decimal place ' 
    else: 
     return 'Error' 
+1

什麼不起作用? – Andrey

回答

1

在每個ifelif語句,改變一個數字正確的字符串:

if corf == "f":   
    return num 
    Celsius = (num - 32) * 5.0/9.0 
    print str(num) + " Fahrenheit is equal to " + str(Celsius)+ " Celsius or " + ('%0.1f degrees Celsius ' %(Celsius)) + 'to one decimal place ' 
elif corf == "c": 
    return num 
    Fahrenheit = (num * 9.0/5.0) + 32 
    print str(num) + " Celsius is equal to " + str(Fahrenheit)+ " Fahrenheit or " + ('%0.1f degrees Celsius ' %(Celsius)) + 'to one decimal place ' 

爲什麼?因爲corf的唯一輸入只應該是「f」或「c」。現在每個條目都應該與正確的代碼相匹配,如上所示。

+0

嗯......這是一個問題,另一個大問題是他在數學之前返回數字。 (還有一個問題,他複製和粘貼不好,影響內置(最大),並沒有顯示調用它經常被人忘記做) – Foon