Table of Contents

About

Python - (Loop|Iteration)

Example

count = 0

if count < 5:
    print "Hello, I am an if statement and count is", count
    
while count < 10:
    print "Hello, I am a while and count is", count
    count += 1
    
loop_condition = True

while loop_condition:
    print "I am a loop"
    loop_condition = False
import random

print "Lucky Numbers! 3 numbers will be generated."
print "If one of them is a '5', you lose!"

count = 0
while count < 3:
    num = random.randint(1, 6)
    print num
    count += 1
    if num == 5:
        print "Sorry, you lose!"
        break
else:
    print "You win!"
    # the else block will execute anytime the loop condition is evaluated to False