2013-05-20 76 views
0

我是新來的Python,我試圖做一個Python程序,如果你想將華氏轉換爲攝氏溫度。下面是程序:攝氏和華氏轉換的選擇

x = (raw_input("would you like to convert fahrenheit(f) or celsius(c)?")) 
if x == "f": 
y = (raw_input("what is the fahrenheit?")) 
f = (int(y) - 32) * 5.0/9 
print f 

if x == "c": 
n = (raw_input("what is the celsius?")) 
z = (int(n) *9)/5 + 32 
print "and in fahrenheit, that is:" 
print z 

我試圖改變if x == "c"elif x == "c",但它給了我一個TextError。有任何想法嗎?

回答

1

你需要要麼刪除:

print f 

呼叫,或移動之後,或縮進它,因爲ELIF具有if塊後,立即來了,並與打印語句縮進它的方式是,它結束了if塊。

3

就縮進print

x = raw_input("would you like to convert fahrenheit(f) or celsius(c)?") 
if x == "f": 
    y = raw_input("what is the fahrenheit?") 
    f = (int(y) - 32) * 5.0/9 
    print f 
elif x == "c": 
    n = raw_input("what is the celsius?") 
    z = (int(n) * 9)/5.0 + 32 
    print "and in fahrenheit, that is:" 
    print z 
1

一個簡單的方法可以是:

x = (raw_input("would you like to convert fahrenheit(f) or celsius(c)? ")) 
if x == "f": 
    y = (raw_input("what is the fahrenheit?")) 
    f = (int(y) - 32) * 5.0/9 
    print "and in celsius, that is: ", 
    print f 
elif x == "c": 
    y = (raw_input("what is the celsius?")) 
    f = (int(y) *9)/5.0 + 32 
    print "and in fahrenheit, that is: ", 
    print f 
else 
    print "Error" 
+0

這將始終打印 「錯誤」。在'print'錯誤''之前你錯過了'else'。 – srikanta

+0

謝謝@srikanta。 Ans更新。 – sinhayash

+1

'else'沒有:following是一個SyntaxError。最近在Lua編碼? ;) – kampu