添加一个循环,直到用户输入有效答案? [英] Add a loop until user inputs a valid answer?
本文介绍了添加一个循环,直到用户输入有效答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为此添加一个循环:
I want to add a loop to this:
question = raw_input("Reboot Y/N ")
if len(question) > 0 and question.isalpha():
answer = question.upper()
if answer == "Y":
print "Reboot"
elif answer == "N":
print "Reboot Cancled"
else:
print "/ERROR/"
因此,如果用户输入任何其他内容,则会出现错误并将其发送回问题。
So if the user inputs anything else the error appears and sends them back to the question.
推荐答案
在顶部添加一个True,如果用户输入了正确的输出,则打破循环: -
Add a while True at the top, and if user has entered correct output, break the loop: -
while True:
question = raw_input("Reboot Y/N ")
if len(question) > 0:
answer = question.upper()
if answer == "Y":
print "Reboot"
break
elif answer == "N":
print "Reboot Canceled"
break
else:
print "/ERROR/"
这篇关于添加一个循环,直到用户输入有效答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文