当我在循环时打字时,为什么所有值都会出现真实?
我目前正在学习Walrus:=,当我进行此编码并添加到列表然后打印时,所有项目都会出现一个列表。
foods = []
while food := input("what food do you like: ") != 'quit':
foods.append(food)
enter code here
print(foods)
I am currently learning walrus := , and when I do this coding and add to the list and then print it, a list appears with all the items True.
foods = []
while food := input("what food do you like: ") != 'quit':
foods.append(food)
enter code here
print(foods)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与关系运营商相比,海象操作员的任务具有较低的优先级。因此说:
评估
输入为
退出
它总是返回true
导致所有值food
为be
true
和食物是所有true
的列表。您可以尝试使用:
The walrus operator assignment has lower precedence as compared to the relational operators. So saying:
Evaluates as
And until the input is
quit
it always returnsTrue
causing all values offood
to beTrue
and foods to be a list of allTrue
.You can try using: