Home > Software engineering >  How to make this loop repeat until right value has been added, Python
How to make this loop repeat until right value has been added, Python

Time:09-17

I'm trying to get the below code to repeat until one of the correct string values is entered.

while True:
    if value in ['man united', 'man city', 'liverpool', 'chelsea']:
        print(tabulate(data[value]))
        break
    if value not in ['man united', 'man city', 'liverpool', 'chelsea']:
        print("You entered a wrong option, Please enter a correct option")
        print(f"1: {options}")

I've tried a couple different ways but can't achieve exactly what I'm looking for. This code is within a python function.

Any help is greatly appreciated. New to Python

CodePudding user response:

Here is how I think you could do it, l is just a list containing the options, but you do not have to use it.

l = ['man united', 'man city', 'liverpool', 'chelsea']
value = input()

while value not in l:
    print("You entered a wrong option, Please enter a correct option")
    print(f"1: {options}")
    value = input() #or however you want to change the value variable

print(tabulate(data[value])) #this will execute only once the correct choice has been selcted
  • Related