Home > Software engineering >  python. simple loop problem. making a pizzaria chatbot. Chatbot takes in toppings from a list one at
python. simple loop problem. making a pizzaria chatbot. Chatbot takes in toppings from a list one at

Time:12-15

The python bot has a list of toppings. If the user enters a valid topping, it should say "This is a valid topping. If not, it says it is not a valid topping. It should repeat to the top of the loop for the user to enter a new topping for their pizza unless the user types 'done'. Later, I am going to record what valid toppings the user entered but for now, I want to fix this kink.

This is my code:

pickingToppings = True

while pickingToppings:
    if whatToppings not in toppings:
        print()
        print("If you are done adding toppings, type 'done'.")
        print()
        print("That is not a valid topping")
        isDone = input("Are you done with your toppings? ").lower()
        
        if isDone == 'yes':
            pickingToppings = False
        
        if isDone == 'no':
            pickingToppings
    
    else:
        print()
        print("This is a valid topping")
        print()
        isDone = input("Are you done with your toppings? ").lower()
        
        if isDone == 'yes':
            pickingToppings = False
        if isDone == 'no':
            pickingToppings

Any thoughts?

CodePudding user response:

Something like this might work:

pickingToppings = True

while pickingToppings:
    whatToppings = input("Pick a topping")
    if whatToppings not in toppings:
       print()
       print("That is not a valid topping")
    else:
       print()
       print("This is a valid topping")
       print()

    isDone = input("Are you done with your toppings? ").lower()
    
    if isDone == 'yes':
        pickingToppings = False

As stated above, the toppings list would need to be defined

CodePudding user response:

Try:

toppings = ["tomato", "onion", "olive", "pepperoni", "mushroom"]
pizza = list()

while True:
    whatToppings = input("Enter a topping. If you are done adding toppings, type 'done'.\n").lower()
    if whatToppings not in toppings:
        if whatToppings == "done":
            print(f"Your pizza will be made with the following toppings: {', '.join(pizza)}")
            break
        print("Your selection is invalid. Try again.\n")
    else:
        print("This is a valid topping. It has been added.")
        pizza.append(whatToppings)
  • Related