My code is:
JavaScript
x
5
1
foods2 = list()
2
while food2 := input("Which food do you like ? ") != "quit":
3
foods2.append(food2)
4
print(foods2)
5
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:
JavaScript
1
5
1
foods2 = list()
2
while (food2 := input("Which food do you like ? ")) != "quit":
3
foods2.append(food2)
4
print(foods2)
5