문제

#!/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)

So my program counts to ten no problem.. But it never display the text when a == 5. Any ideas? This is my first attempts at python.

도움이 되었습니까?

해결책

Your if code should be inside your while loop:

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top