My code is:
foods2 = list()
while food2 := input("Which food do you like ? ") != "quit":
foods2.append(food2)
print(foods2)
I want the output to be a list from my inputs.
Why is the output [True, True, True] when there are three inputs before typing ‘quit’?
Advertisement
Answer
Looking at Operator precedence, the assignment operator := is the lowest of them all. That means that python evaluated the != first and assigned its boolean result to food2. You can use parentheses to change evaluation order:
foods2 = list()
while (food2 := input("Which food do you like ? ")) != "quit":
foods2.append(food2)
print(foods2)