【问题标题】:Python "while x:" statement [duplicate]Python“while x:”语句[重复]
【发布时间】: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:

【问题讨论】:

标签: python while-loop


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在布尔值中,无零值表示“真”,0 表示“假”。代码到达参数 while(0) 或 while(false) 并终止正文

    【讨论】:

      猜你喜欢
      • 2019-02-05
      • 2021-02-07
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多