[英]Force user to input only Y or N in Python
I'm new in programming and my first assessment is creating a program that asks a few questions to the user.我是编程新手,我的第一个评估是创建一个向用户提出几个问题的程序。 The first question requires the user to answer Y or N and I would like to force the user to only do it, by keep asking them the same question until the user type Y or N, without accepting any other letter or number or symbols.第一个问题要求用户回答 Y 或 N,我想强制用户只做它,不断问他们同样的问题,直到用户输入 Y 或 N,不接受任何其他字母或数字或符号。 If the answer in N, then the user is asked another question (which is apparently working for me) and so on.如果答案是 N,那么用户会被问到另一个问题(这显然对我有用)等等。
I am trying to use while loop and now I'm lost:我正在尝试使用 while 循环,现在我迷路了:
print("Phase vaccine rollout (PVR) v1.0")
print("==========================")
phase1a = input("Are you a quarantine and border worker,\n\
prioritised frontline healthcare worker, or\n\
an aged care/disability care staff member or resident (Y/N)?: ")
while phase1a.upper() == "Y" or "N":
if phase1a.upper() == "Y":
print()
print("Vaccines will be made available to you in Phase 1a")
print()
exit()
elif phase1a.upper() == "N":
print("__________________________")
phase1b = input("Are you a health care worker, or\n\
a critical or high risk worker (including defence,\n\
police, fire, emergency services and meat processing (Y/N)?: ")
continue
else:
break
# --- and from here I will continue with the phase1b... # --- 从这里我将继续阶段1b ...
Thank you!谢谢!
Instead of while phase1a.upper() == "Y" or "N":
, you could write:而不是while phase1a.upper() == "Y" or "N":
,你可以写:
while phase1a.upper() != "Y" and phase1a.upper() != "N":
print("Invalid input. Please enter "Y" or "N".)
phase1a = input("Are you a quarantine and border worker,\n\
prioritised frontline healthcare worker, or\n\
an aged care/disability care staff member or resident (Y/N)?: ")
You will have to restructure your if
statement a bit but hopefully this shows you the idea.您将不得不稍微重组您的if
语句,但希望这可以向您展示这个想法。 This will force the user to keep entering input until he/she enters "Y" or "N".这将迫使用户继续输入,直到他/她输入“Y”或“N”。
Using a while True
loop will do the trick....使用while True
循环可以解决问题....
print("Phase vaccine rollout (PVR) v1.0")
print("==========================")
while True:
phase1a = input("Are you a quarantine and border worker,\n\
prioritised frontline healthcare worker, or\n\
an aged care/disability care staff member or resident (Y/N)?: ")
if phase1a.upper() == "Y":
print()
print("Vaccines will be made available to you in Phase 1a")
print()
exit()
elif phase1a.upper() == "N":
print("__________________________")
phase1b = input("Are you a health care worker, or\n\
a critical or high risk worker (including defence,\n\
police, fire, emergency services and meat processing (Y/N)?: ")
exit()
else:
continue
# --- and from here I will continue with the phase1b...
We will be using while True that will end with a else statement which will ask for the user to repeat their input if it is not Y/N.我们将使用 while True ,它将以 else 语句结束,如果不是 Y/N,它将要求用户重复他们的输入。 Also to note: You will have to add the result if the user inputs N.另请注意:如果用户输入 N,则必须添加结果。
print("Phase vaccine rollout (PVR) v1.0")
print("==========================")
phase1a = input("Are you a quarantine and border worker,\n\
prioritised frontline healthcare worker, or\n\
an aged care/disability care staff member or resident (Y/N)?: ")
while True:
if phase1a.upper() == "Y":
print("\nVaccines will be made available to you in Phase 1a\n")
exit()
elif phase1a.upper() == "N":
print("__________________________")
phase1b = input("Are you a health care worker, or\n\
a critical or high risk worker (including defence,\n\
police, fire, emergency services and meat processing (Y/N)?: ")
break
else:
phase1a = input("\nAre you a quarantine and border worker,\n\
prioritised frontline healthcare worker, or\n\
an aged care/disability care staff member or resident (Y/N)?: ")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.