I am trying to create a python program but i have a question about a certain part of the loop for example i have

question = input("press 1 or 2")
if statement == 1:
      print "somthing"

elif statement == 2:
      print "abc"

ok my question now is for example the user were to press 10, python would just skip the whole if loop, how can i make the message reappear and make the user chose the correct options?

Recommended Answers

All 3 Replies

The "standard" way is to use a while() loop.

question=0
while question not in ['1'. '2']:
    question = input("Press 1 or 2")

print("1 or 2 entered", question)

I am trying to create a python program but i have a question about a certain part of the loop for example i have

question = input("press 1 or 2")
if statement == 1:
      print "somthing"

elif statement == 2:
      print "abc"

ok my question now is for example the user were to press 10, python would just skip the whole if loop, how can i make the message reappear and make the user chose the correct options?

First if is not a loop, it is control structure.
Secondly you need to get a book and learn Python. see one of many free PyBooks here

Hello.

You can use the else statement and enter what ever you want, but first make a function and put the if statement that you have in it, so that you can call it back if the user enters other than 1 or 2 as in your example.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.