Issue
I'm new to programming and my first assessment is to create 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 types "Y" or "N", without accepting any other letters or numbers or symbols.
If the answer in "N", then the user is asked another question (which is apparently working for me) and so on.
I am trying to use a while loop and now I'm lost:
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...
Solution
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. Also to note: You will have to add the result if the user inputs 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)?: ")
Answered By - im3dabasia
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.