我有一个while循环,我想限制其中一个答案。一个人会继续循环而另一个会突破它。问题是代码类似于以下内容:
choice = "y"
while choice == "y":
if condition:
print ("something")
choice = input("continue?")
我想只允许y或n个答案,否则应该再次提示。请帮忙。我发现有几件事情很接近,但是当我尝试时,我会得到一个无限循环的提示。
答案 0 :(得分:1)
你可以使用numpy.append()
。循环不会停止,直到它收到输入' Y'或者' N'来自用户。试试这个:
while True
答案 1 :(得分:0)
我认为您正在寻找sentinel controlled program
。循环将一直询问,直到您输入标记值,' no'。
while (c != 'no'):
... c = input("continue?")
... if 2 > 1:
... print('It is')
答案 2 :(得分:0)
试试这个。它接受是/是/是/是/否/否/否/否/否/ n / N
valid=['Y','N']
while True:
choice = str(raw_input('continue?'))
if choice.upper()[0] in valid:
print(choice)
print ("Write your code here")
break
else:
continue