2012-04-12 149 views
1

我在最後一行第14行出現語法錯誤。我看不出爲什麼,因爲它似乎是一個簡單的打印語句。這個Python代碼中的語法錯誤在哪裏?

cel = "c" 
far = "f" 
cdegrees = 0 
fdegrees = 0 
temp_system = input ("Convert to Celsius or Fahrenheit?") 
if temp_system == cel: 
    cdegrees = input ("How many degrees Fahrenheit to convert to Celsius?") 
    output = 5/9 * (fdegrees - 32) 
    print "That's " + output + " degrees Celsius!" 
elif temp_system == far: 
    fdegrees = input ("How many degrees Celsius to convert to Fahrenheit?") 
    output = (32 - 5/9)/cdegrees 
    print "That's " + output + " degrees Fahrenheit!" 
else print "I'm not following your banter old chap. Please try again." 
+5

請總是包含錯誤的完整回溯。 – 2012-04-12 15:28:22

回答

8

您忘記了最後else後的冒號(:)。

另外:

input ("Convert to Celsius or Fahrenheit?") 

應改爲

raw_input ("Convert to Celsius or Fahrenheit?") 

input()嘗試同時raw_input需要一個 '原始' 的字符串來評價它的輸入。例如,如果輸入cinput()中,它會嘗試評估表達式c,就好像它是尋找變量c的Python代碼,而raw_input只是簡單地接收字符串而不嘗試對其進行評估。

另外,您不能像在這種情況下那樣使用整數連接(加在一起)字符串,其中output是一個數字。

將其更改爲

print "That's " + str(output) + " degrees Celsius!" 

print "That's %d degrees Celsius!" % output 
+0

哦對不起意思'冒號'哈哈 – jamylak 2012-04-12 15:29:04

+0

謝謝 - 我沒有意識到:是可選的。 雖然我仍然在這個簡單的程序中出現錯誤。我想在我輸入第一個輸入提示後選擇攝氏度:c。 回溯(最近通話最後一個): 文件 「」,5號線,在 文件 「」,1號線,在 NameError:名字 'C' 是沒有定義 我沒這個定義在開始時? – Calydon 2012-04-12 15:56:59

+0

我也加了那部分。現在我意識到還有很多其他的錯誤... – jamylak 2012-04-12 16:01:18