python how can I print the item in a list?

Question:

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)
Asked By: Snickers

||

Answers:

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

Answered By: Dmitriy Neledva

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 ().

Answered By: Swifty
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.