How to make a loop in Python that asks for the user to type this or that and they get a different response for both, and if it's invalid it loops?

Question:

New to Python. Working on a program and I want to add a loop so that if they don’t put in 1 or 2 then they are asked to try again until they do it right. In my assignment, I’m making a cafe. I want to ask the user if they are dining in or ordering take out (1 or 2) and each option offers a different response. 1 for example would output "Thank you for dining in with us" and 2 would output: "Thank you for choosing take out". If they put anything other than 1 or 2, I want it to prompt them to try again until they put the right response in. This would be a loop right?

This is what I have so far and the loop part I have is a complete mess and doesn’t run at all.

print("Will you be dining in or ordering take out?")
while True:
    try:
        where = input("For Dine In, Type 1. For Take Out, Type 2: ")
        if where == "1":
            print("Thank you for dining in with us. Please fill out the following: ")
        elif where == "2":
            print("Thank you for choosing take out. Please fill out the following: ")
        else:
            print("Invalid response, please type 1 or 2: ")  
        except 
            

What am I doing wrong?

Asked By: Erin McKee

||

Answers:

Try this:

print("Will you be dining in or ordering take out?")
while True:
    try:
        where = input("For Dine In, Type 1. For Take Out, Type 2: ")
        if where == "1":
            print("Thank you for dining in with us. Please fill out the following: ")
        elif where == "2":
            print("Thank you for choosing take out. Please fill out the following: ")
        else:
            print("Invalid response, please type 1 or 2: ")  
    except:
        pass
Answered By: LhasaDad

The error in your code is that except is supposed to have a block after it — but you do not need try/except at all since nothing you’re doing is expected to raise an exception. Just break the loop once you have a valid response.

print("Will you be dining in or ordering take out?")
while True:
    where = input("For Dine In, Type 1. For Take Out, Type 2: ")
    if where == "1":
        print("Thank you for dining in with us. Please fill out the following: ")
        break
    elif where == "2":
        print("Thank you for choosing take out. Please fill out the following: ")
        break
    else:
        print("Invalid response, please type 1 or 2: ")
Answered By: Samwise