【发布时间】:2017-09-04 23:15:37
【问题描述】:
为什么x到达0时会退出下面的while循环?
x = 1
while x:
print(x)
x -= 1
它只打印1。 while 语句不应该类似于:while x "is something": 而不仅仅是while x:?
【问题讨论】:
-
因为整数对象
0在布尔上下文中计算为False:bool(0) == False。 Python 中的所有对象要么是“真”,要么是“假”。
标签: python while-loop