lundi 28 juin 2021

Python - printing message if something in a list is not working

Screenshot from shell of this code not working I've been banging my head against the wall for over an hour on this super simple problem, but for the life of me, I cannot make it work. All I want is this ultra simple program to return the line "blah blah has already been added" if the user input IS in the list already. I've tried the common sense "if blah in blah_blah: print("blah blah)" but python is just pretending like that doesn't work. I already tried "if blah in blah_blah and blah not in blah_blah_blah" but python continues to pretend that doesn't work either. I'm going insane with this problem that makes zero sense to my still learning, rookie brain. Please help me. Here is my super stupidly simple code, including two super common sense ways to accomplish my problem. I've left both in because neither one does literally anything anyway, so python apparently doesn't care what I've typed at this point:

prompt = "\nEnter a topping for your pizza:"

prompt += "\n(Enter 'done' when you've finished) "

pizza_toppings = []

available_toppings = ['pepperoni','extra cheese','black olives','red onions',
    'green peppers','sausage']

while True:

    topping = input(prompt)

    if topping == 'done':
        break
    elif topping in pizza_toppings:
        print(f"{topping} has already been added.")
    else:

        if topping in available_toppings and topping not in pizza_toppings:
            pizza_toppings.append(topping)
        elif topping not in available_toppings:
            print(f"{topping} is not an available topping.")
        else:
            print(f"{topping} has already been added.")

print(f"Your selected toppings are: {pizza_toppings}")

No matter what I type, this program still lets me enter the same topping 50 times, and will add it to the list pizza_toppings every single time. Why isn't my program catching this?

Aucun commentaire:

Enregistrer un commentaire