-2

In my program I have a menu that looks like this:

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 ----------------->>>>>")) 
except ValueError: 
    print("Please enter one of the menu choices above, TRY AGAIN ") 

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.

Please dont roast me.

Thanks

  • why do poeple keep saying that my posts are unuseful? you guys are all like pro coders this would take you like twio seconds – PortableGibbon Oct 16 '16 at 11:04
  • *two........... – PortableGibbon Oct 16 '16 at 11:04
  • You may find the top answer here helpful: [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response). Also, you should fix the indentation in your code: correct indentation is _vital_ in Python. BTW, if you notice a mistake in a comment you just made you have a few minutes in which you can edit the comment. – PM 2Ring Oct 16 '16 at 11:07
  • See the function `sanitised_input` in [this question](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response): just use `MenuChoice = sanitised_input('Please enter your choice here ----------------->>>>>', ['1','2','3','4','5','x','X'])`. Your downvotes are because this is a duplicate question, already asked and answered. – Rory Daulton Oct 16 '16 at 11:09
  • `str(input(` is pointless: `input()` always returns a string. And it won't raise a `ValueError`, no matter what the user types in. – PM 2Ring Oct 16 '16 at 11:10

2 Answers2

1

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

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 <= MenuChoice <= 4):
    MenuChoice = input("Please enter your choice here ----------------->>>>>")
    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

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

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • @RoryDaulton Fixed – TerryA Oct 16 '16 at 11:14
  • thaks so much for all your support guys I really wouldnt be where I am today without you – PortableGibbon Oct 16 '16 at 14:03
  • I am trying to do this now: – PortableGibbon Oct 16 '16 at 14:38
  • MenuChoice = 0 print("Type 1 to enter option 1") print("Type 2 to enter option 2") print("Type 3 to enter option 3") print("Press x to quit") while not (1 <= MenuChoice <=4): MenuChoice = input("Please enter your choice here ----------------->>>>>") if MenuChoice == 'x': break try: MenuChoice = int(MenuChoice) except ValueError: print("Please enter one of the menu choices above, TRY AGAIN ") MenuChoice = 0 if MenuChoice == "1": print("hello") – PortableGibbon Oct 16 '16 at 14:40
  • Ignore all the above, when I have finished using menu option 1, It finshes the prgram when what I actually want is for it to loop back and re print the menu so that options 2-5 can be used – PortableGibbon Oct 16 '16 at 14:48
  • I am using your code @TerryA but how would I loop the menu so that after option 1 has been selected the menu prints again so that the user can select another option? – PortableGibbon Oct 17 '16 at 14:59
  • @PortableGibbon set it in a function and call it each time at the end – TerryA Oct 17 '16 at 20:40
0

I think he needs a While true loop . Did using python 2.X

import time
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 True:
    try:
        print ("Only Use number 1 to 4 or x to Quit... Thanks please try again")
        MenuChoice = raw_input("Please enter your choice here ----------------->>>>> ")
        try:
            MenuChoice = int(MenuChoice)
        except:
            MenuChoice1 = str(MenuChoice)
        if MenuChoice1 == 'x' or 1 <= MenuChoice <= 4:
            print "You selected %r Option.. Thank you & good bye"%(MenuChoice)
            time.sleep(2)
            break
    except:
        pass
Shekhar Samanta
  • 875
  • 2
  • 12
  • 25