Question

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

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top