当我在循环时打字时,为什么所有值都会出现真实?

发布于 2025-01-23 05:52:30 字数 202 浏览 2 评论 0原文

我目前正在学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2025-01-30 05:52:30

与关系运营商相比,海象操作员的任务具有较低的优先级。因此说:

food := input("what  food do you like: ") != 'quit':

评估

food = <result of (input("what  food do you like: ") != 'quit')>

输入为退出它总是返回true导致所有值foodbe true和食物是所有true的列表。
您可以尝试使用:

(food := input("what  food do you like: ")) != 'quit':

The walrus operator assignment has lower precedence as compared to the relational operators. So saying:

food := input("what  food do you like: ") != 'quit':

Evaluates as

food = <result of (input("what  food do you like: ") != 'quit')>

And until the input is quit it always returns True causing all values of food to be True and foods to be a list of all True.
You can try using:

(food := input("what  food do you like: ")) != 'quit':
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文