I am having trouble vaildating my code.I need my code to loop untill a valid answer is inputted

前端 未结 4 1125
广开言路
广开言路 2021-01-25 05:34

I am struggling with this piece of Python code. The problem is,when a user enters something wrong I need my code to keep looping until they input a valid answer. This is how the

4条回答
  •  死守一世寂寞
    2021-01-25 06:13

    Use a while loop and break once you're satisfied:

    drink = None
    while drink is None:
        choice = input("Please enter a drink from the menu above\n").lower()
    
        if choice == "1" or choice == "fanta":
            drink = "Fanta"
        elif choice == "2" or choice == "coke":
            drink = "Coke"
        elif choice == "3" or choice == "pepsi":
            drink = "Pepsi"
        elif choice == "4" or choice == "sprite":
            drink = "Sprite"
    
    Order[DRINK] = drink
    

提交回复
热议问题