【问题标题】:Python Simple ifPython 简单的 if
【发布时间】:2014-05-03 15:02:46
【问题描述】:
#!/usr/bin/python
import time
a = 0
if a == 5:
    print"Congrats you hit five!"

while a <10:
    a = a + 1
    print a
    time.sleep(.5)

所以我的程序数到十没问题。但是当 a == 5 时它从不显示文本。 有任何想法吗?这是我第一次尝试python。

【问题讨论】:

    标签: python if-statement printing while-loop


    【解决方案1】:

    您的if 代码应该您的while 循环中:

    import time
    a = 0
    
    while a < 10:
        a = a + 1
        print a
        if a == 5:
            print"Congrats you hit five!"
        time.sleep(.5)
    
    [OUTPUT]
    1
    2
    3
    4
    5
    Congrats you hit five!
    6
    7
    8
    9
    10
    

    【讨论】:

      猜你喜欢
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2020-02-22
      • 2020-04-08
      • 2020-08-15
      相关资源
      最近更新 更多