0

I have tried so many variations here to no avail. I have a menu with 4 options, I'm trying to loop if options 1, 2, 3 or 4 is not selected whilst printing out a response and reprompting a choice of only 1, > 2, 3 or 4. I've tried:

print("Options:")
print("Option 1")
print("Option 2")
print("Option 3")
print("Option 4")
choice = int(input("What would you like to choose [1,2,3,4]? "))
while choice not in ['1','2','3','4']:
    print: ("Please enter 1, 2, 3 or 4. ")
    choice = int(input("What would you like to choose [1,2,3,4]? "))

and

print("Options:")
print("Option 1")
print("Option 2")
print("Option 3")
print("Option 4")
choice = input("What would you like to choose [1,2,3,4]? ")
while choice not '1' and choice not '2' and choice not '3' and choice not '4':
    print: ("Please enter 1, 2, 3 or 4. ")
    selection = input("What would you like to choose [1,2,3,4]? ")

and

print("Options:")
print("Option 1")
print("Option 2")
print("Option 3")
print("Option 4")
choice = int(input("What would you like to choose [1,2,3,4]? "
    while choice != '1' and user_input != '2' and user_input != '3' and choice != 4:
    choice = int(input("Please enter 1, 2, 3 or 4. "))

I'm getting a range or errors from syntax errors to incorrect output (will not print "Please enter 1, 2, 3 or 4. " and will not loop. Only after something very simple, don't want to use a quit statement - where am I going wrong here please?

YVB
  • 11
  • 3
  • Use this `while choice!=1 and choice!=2 and choice!=3 and choice!=4:` – dt170 May 14 '20 at 05:57
  • Hi @dt170, yeah, I'd tried that earlier too - but when I do that, it also ignores the print command before requesting re-input? – YVB May 14 '20 at 06:52

1 Answers1

0

You are requesting an integer and you are looking for it as char, change your validation list to integers and your first option works:

print("Options:")
print("Option 1")
print("Option 2")
print("Option 3")
print("Option 4")
choice = int(input("What would you like to choose [1,2,3,4]? "))
while choice not in [1,2,3,4]:
    #print: ("Please enter 1, 2, 3 or 4. ")
    print("Please enter 1, 2, 3 or 4. ")
    choice = int(input("What would you like to choose [1,2,3,4]? "))

This work

MarvikDC
  • 16
  • 1
  • Hi @ Marvik, I had tried that solution too - but when I run the module and enter '5', for example, it ignores the command to print "please enter 1, 2 3 or 4" and just requests int input again, so I thought that made the print command obsolete for some reason? – YVB May 14 '20 at 06:52
  • helps if I @ you properly! – YVB May 14 '20 at 07:40
  • @YVB It is for the two points after the print, they are not necessary – MarvikDC May 14 '20 at 13:38
  • I had to restart the kernel to be able to run it again, maybe you need it too – MarvikDC May 14 '20 at 13:45