1

I am quite a beginner at using Python. I am trying to create a loop for a series of questions. What I want it to do is ask the user to input a number corresponding with an option provided, range being 1-4. If the user enters a number between them, it records it and carries onto next question. However, even if the user types anything else, it will display that it is incorrect (as I had used an else statement), but instead of re-asking the same question, it simply moves on. Could someone point me in the right direction please?

Here is a sample code.

print()
print("Please select the module ")
print("Press 1 for")
print("Press 2 for")
print("Press 3 for")
print("Press 4 for")

choice = input("> ")
if choice == '1':
 buddy.module = ("JP")
elif choice == '2':
 buddy.module = ("ID")
elif choice == '3':
 buddy.module = ("MC")
elif choice == '4':
 buddy.module = ("MC")
else:
 print("Incorrect option")

1 Answer 1

3

You can do a while loop here something like:

choice = 0
while choice not in['1','2','3','4']:
    print()
    print("Please select the module ")
    print("Press 1 for")
    print("Press 2 for")
    print("Press 3 for")
    print("Press 4 for")
    choice = input("> ")
    if choice == '1':
       buddy.module = ("JP")
    elif choice == '2':
       buddy.module = ("ID")
    elif choice == '3':
       buddy.module = ("MC")
    elif choice == '4':
       buddy.module = ("MC")
    else:
       print("Incorrect option")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.