-2

I need a validation loop, I don't want the program to move to the next line without the validation. This is just a rough script. In all whatever the program maybe I want it to validate an individual input and keep looping back if the correct information or typo is written.

    choice = ['A','E','C']
    size = ['S','L','M']

    drinkChoice = input('Enter drink choice here ')
    drinkSize = input('Enter drink size ')

    while True:
            if drinkChoice not in choice:
                    print('choose from the available choices A,E,C')
                    drinkChoice = input('Enter drink choice here ')

            if drinkSize not in size:
                    print('Please chose M, L and S are the choices')
                    drinkSize = input('Enter drink size ')
                    continue
            else:
                    #THIS IS JUST A TEST
                    print('You order total is')
                    break
bart
  • 1,003
  • 11
  • 24
Tjs 01
  • 13
  • 1
  • 6

2 Answers2

0

click provides a straightforward solution for doing just this:

drinkChoice = click.prompt("Enter drink choice here: ",
                           type=click.Choice(["A", "E", "C"], case_sensitive=False),
                           show_choices=True)
drinkSize = click.prompt("Enter drink size: ",
                         type=click.Choice(["S", "M", "L"], case_sensitive=False),
                         show_choices=True)
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • i installed click through pip but can't seem to import it to make your script run. Will try different ways and i will let you know if it works – Tjs 01 Jul 16 '19 at 23:21
0

Modified version of your while loop

choice = ['A','E','C']
size = ['S','L','M']





while True:
    drinkChoice = input('Enter drink choice here: ')

    if drinkChoice not in choice:
        print('choose from the available choices A,E,C')
    else:
        break

while True:
    drinkSize = input('Enter drink size: ')
    if drinkSize not in size:
        print('Please chose M, L and S are the choices')
    else:
        break
SmitM
  • 1,366
  • 1
  • 8
  • 14