Python If-Else语句语法错误
我只是在玩弄If-Else语句,我想知道如何让它循环来再次问这个问题,如果第一个If-Else是真的 此外,您将如何在末尾添加一个通用的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
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“哈哈,杰克比你强
需要一个结束语。是的,对不起,我已经纠正了任何其他错误,但是谢谢:)