0

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. 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 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...

Thank you!

Milodoc
  • 1
  • 3
  • `while phase1a.upper() == "Y" or "N"` is not the right way to check for multiple values. See https://stackoverflow.com/q/15112125/494134 – John Gordon Aug 01 '21 at 02:37
  • Does this answer your question? [Why does \`a == x or y or z\` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true) – Tomerikoo Aug 01 '21 at 16:02

3 Answers3

1

Using a while True loop will do the trick....

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...
CYBERDEVILZ
  • 315
  • 1
  • 11
0

Instead of while phase1a.upper() == "Y" or "N":, you could write:

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. This will force the user to keep entering input until he/she enters "Y" or "N".

purple
  • 212
  • 1
  • 8
  • 1
    `while phase1a.upper() != "Y" or phase1a.upper() != "N"` will always be true, no matter what is entered. You meant to use `and` insead of `or`. – John Gordon Aug 01 '21 at 02:40
0

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)?: ")
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
im3dabasia
  • 48
  • 10