Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python If-Else语句语法错误_Python_Loops_Generics_If Statement - Fatal编程技术网

Python If-Else语句语法错误

Python If-Else语句语法错误,python,loops,generics,if-statement,Python,Loops,Generics,If Statement,我只是在玩弄If-Else语句,我想知道如何让它循环来再次问这个问题,如果第一个If-Else是真的 此外,您将如何在末尾添加一个通用的Else,使其对输入的任何其他名称都有意义 我使用的是python2.7上的Raspberry Pi。 #!/usr/bin/python name="" while name !="quit": name = raw_input("What is your name? ") print "Hello", name import time time.s

我只是在玩弄If-Else语句,我想知道如何让它循环来再次问这个问题,如果第一个If-Else是真的

此外,您将如何在末尾添加一个通用的Else,使其对输入的任何其他名称都有意义

我使用的是
python2.7
上的
Raspberry 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"

你错过了这一行的结束语
print“Jack,我认为你很棒。”
最后一行的
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
 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

你忘了在
print“哈哈,杰克比你强
行中加括号了

您应该只导入时间一次,在while循环之外,最好是在脚本开始时。您不需要在name!=“quit”时导入时间

if/else
构造应如下所示:

if something:
    #do something

elif otherthing:
    #do something else

else:
    #if everything above fails, do this
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

您可以使用
continue
将执行返回到循环的顶部

 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
 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子句,所以泛型catch-all有点困难。您可以在末尾这样检查:

if something:
    #do something

elif otherthing:
    #do something else

else:
    #if everything above fails, do this
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

print“哈哈,杰克比你强
需要一个结束语。是的,对不起,我已经纠正了任何其他错误,但是谢谢:)