Home > database >  python how can I print the item in a list?
python how can I print the item in a list?

Time:10-19

the problem I'm having is that when I try to print what's inside the list I get "True" instead of what the item is.

Here is the code I have:

    foods = []

    while food := input("enter foods ") !="exit":
        foods.append(food)
    
    for food in foods: 
        print(food)

CodePudding user response:

foods = []

    while food := input("enter foods "):
        if food == "exit":
            break
        foods.append(food)
    
    for food in foods: 
        print(food)

you can try something like this

CodePudding user response:

Another way to do this:

def list_creator():
    while (food := input("Enter food: ")) != "exit":
        yield food

foods = [i for i in list_creator()]


for food in foods: 
    print(food)

But yes, as was said in comments, your main problem was caused by not encapsulating your variable assignment in ().

  • Related