How to accept only 5 possible inputs
<p>In my program I have a menu that looks like this:</p>

<pre><code>MenuChoice = ''
while MenuChoice != 'x':
print("Type 1 to enter the first option")
print("Type 2 to enter the second option")
print("Type 3 to enter the third option")
print("Type 4 to enter the fourth option")
print("Press x to quit")
try: 
    MenuChoice = str(input("Please enter your choice here -----------------&gt;&gt;&gt;&gt;&gt;")) 
except ValueError: 
    print("Please enter one of the menu choices above, TRY AGAIN ") 
</code></pre>

<p>I just want to know a way in which I can insure that only the numbers 1 to 5 are accepted and that if anything else is entered then the program asks the question again.</p>

<p>Please dont roast me.</p>

<p>Thanks</p>

<p>You're right to use a <code>while</code> loop, but think of what condition you want. You want only the numbers 1-5 right? So it would make sense to do:</p>

<pre><code>MenuChoice = 0
print("Type 1 to enter the first option")
print("Type 2 to enter the second option")
print("Type 3 to enter the third option")
print("Type 4 to enter the fourth option")
print("Press x to quit")
while not (1 &lt;= MenuChoice &lt;= 4):
    MenuChoice = input("Please enter your choice here -----------------&gt;&gt;&gt;&gt;&gt;")
    if MenuChoice == 'x' : break
    try:
        MenuChoice = int(MenuChoice)
    except ValueError:
        print("Please enter one of the menu choices above, TRY AGAIN ") 
        MenuChoice = 0 # We need this in case MenuChoice is a string, so we need to default it back to 0 for the conditional to work
</code></pre>

<p>We make our input an integer so that we can see if it's between 1-5. Also, you should put your beginning print statements outside of the loop so it doesn't continually spam the reader (unless this is what you want).</p>

python error-handling menu