2014-06-30 230 views
1

我只是玩弄If Else語句,我想知道如何讓它循環再次問這個問題,如果第一個If Else是真的。Python如果Else語句語法錯誤

此外,你會如何在最後添加一個通用的Else以使其對於放入的任何其他名稱有所說明?

我在Python 2.7Raspberry Pi.

#!/usr/bin/python 

name="" 
while name !="quit": 
name = raw_input("What is your name? ") 
print "Hello", name 
import time 
time.sleep(2) 
if name == "Jack": 
    print "Jack, I have decided that you are awesome." 
    time.sleep(2) 
    print "And I am a computer so I cannot be wrong" 
else: 
    print "Oh, you are not Jack?" 
    time.sleep(2) 
    print "Haha, Jack is better than you" 
if name == "Ronan": 
    print "Ronan, I know everything about you." 
    time.sleep(2) 
    print "For example, I know you hate cats." 
else: 
    print "Oh, you are not Ronan?" 
    time.sleep(2) 
    print "Perhaps you can convince him that he loves cats" 
+2

'打印「哈哈,傑克優於you'需要一個最終報價 –

+0

是的,對不起,我已經固定的任何其他錯誤,但由於:) – Ferex

回答

0

你錯過了這條線print "Jack, I have decided that you are awesome."上最後一行的break命令關閉的報價將有助於終止循環。

name="" 
while name !="quit": 
name = raw_input("What is your name? ") 
print "Hello", name 
import time 
time.sleep(2) 
if name == "Jack": 
    print "Jack, I have decided that you are awesome." 
    time.sleep(2) 
    print "And I am a computer so I cannot be wrong" 
else: 
    print "Oh, you are not Jack?" 
    time.sleep(2) 
    print "Haha, Jack is better than you" 
if name == "Ronan": 
    print "Ronan, I know everything about you." 
    time.sleep(2) 
    print "For example, I know you hate cats." 
else: 
    print "Oh, you are not Ronan?" 
    time.sleep(2) 
    print "Perhaps you can convince him that he loves cats" 
if name != "jack" and name != "Ronan": 
    print "I am not wrong" 

break 
1

您忘記關閉print "Haha, Jack is better than you行的括號​​。

您應該只有import time一次,out of while循環,最好是在腳本的開始處。你不需要導入它while name !="quit"

而且if/else建設應該是這樣的:

if something: 
    #do something 

elif otherthing: 
    #do something else 

else: 
    #if everything above fails, do this 
0

您可以使用continue將返回執行到循環的頂部。

if name == "Jack": 
    print "Jack, I have decided that you are awesome." 
    time.sleep(2) 
    print "And I am a computer so I cannot be wrong" 
    continue 

但是真的需要重構if ... else語句。

而且你的循環結束的邏輯是不完全正確,這將是更好的:

while True: 
    name = raw_input("What is your name? ") 
    if name.lower() in ('quit', 'exit', 'end', 'bye'): 
     break 

    print "Hello", name 
    if name == "Jack": 
    . 
    . 
    . 

至於你的if語句,因爲你有else子句每個名稱,一個通用的包羅萬象的稍難。您可以檢查在這樣的結尾:

KNOWN_NAMES = ('Jack', 'Ronan', 'Whomever') 
QUIT_VERBS = ('quit', 'q', 'exit', 'end', 'bye') 

while True: 
    name = raw_input("What is your name? ") 
    if name.lower() in QUIT_VERBS: 
     break 

    if name == "Jack": 
     print "Jack, I have decided that you are awesome." 
     time.sleep(2) 
     print "And I am a computer so I cannot be wrong" 
    else: 
     print "Oh, you are not Jack?" 
     time.sleep(2) 
     print "Haha, Jack is better than you" 
    if name == "Ronan": 
     # etc, etc 

    if name not in KNOWN_NAMES: 
     print 'I know nothing about no "%s"' % name