如何在用户输入特定字符串时退出 while cicle,在 JES 中,python 2.7
How to exit a while cicle when user input a specific string, in JES, python 2.7
在python中,我想退出一个while cicle,当在cicle中,我会输入一个特定的字符串。
`while list!=[] or reply!="yes":
# @param list: list; list which contains strings of questions
time.sleep(0.9)
question=random.choice(list)
print question
print " "
time.sleep(0.5)
reply=raw_input("reply please: ")
list.remove(question)
问题是:如果我回复 "yes" 回答 while cicle 继续,并打印下一个问题。问题出在哪里?
您正在查找 break
语句:
if reply=='yes':
break
您需要将 while 条件更改为 and
而不是 or
。对于 or
,当列表中有更多项目时循环继续。
您还需要将 reply
初始化为 "yes" 以外的任何值,以便代码进入 while 循环。
在python中,我想退出一个while cicle,当在cicle中,我会输入一个特定的字符串。
`while list!=[] or reply!="yes":
# @param list: list; list which contains strings of questions
time.sleep(0.9)
question=random.choice(list)
print question
print " "
time.sleep(0.5)
reply=raw_input("reply please: ")
list.remove(question)
问题是:如果我回复 "yes" 回答 while cicle 继续,并打印下一个问题。问题出在哪里?
您正在查找 break
语句:
if reply=='yes':
break
您需要将 while 条件更改为 and
而不是 or
。对于 or
,当列表中有更多项目时循环继续。
您还需要将 reply
初始化为 "yes" 以外的任何值,以便代码进入 while 循环。