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

前端 未结 4 1076
广开言路
广开言路 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:19

    As an example, for the drink part to continually loop over until the user enters something valid:

    while choice != "1" or choice != "2" or choice != "3" or choice != "4":
        choice = input("Please enter a valid drink: ")
    if choice == "1" or choice == "fanta":
        Order[DRINK] = "Fanta"
        if choice == "2" or choice == "coke":
            Order[DRINK] = "Coke"
        if choice == "3" or choice == "pepsi":
            Order[DRINK] = "Pepsi"
        if choice == "4" or choice == "sprite":
            Order[DRINK] = "Sprite"
    

    I think this would work because it would keep iterating over the loop until the condition is met. And then once the loop is completed, it will execute the code below it.

提交回复
热议问题