2014-06-30 232 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