Python "while x:"语句

标签 python while-loop

为什么当 x 时退出下面的 while 循环达到0

x = 1
while x:
    print(x)
    x -= 1

它只打印 1 。 while 语句不应该是这样的: while x "is something":不仅仅是while x:

最佳答案

因为 bool(0) => Falsebool(x) for x!=0 => True,所以这就像是说 while x! =0while x>0 在您的情况下。

关于Python "while x:"语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46045023/

相关文章:

python - 发送数据包并更改其源 IP

python - 为什么每当我刷新随机森林回归器时。 MSE 和 MAE 发生变化吗?为什么它会有所不同并取决于什么?

bash - 为什么管道输入到 "read"仅在馈入 "while read ..."构造时才有效?

java - while 循环再次开始后不要求输入

Java 的 .sort() 方法打破了 while 循环

python - 更改特定文本的字体大小 - Python/Pygame

python - 我无法在数据框中添加两列

python - Numpy reshape 矩阵

c - 为什么即使值为真,我的 while 循环也会继续执行?

python - 如何在满足 while 循环条件时跳出 time.sleep()