While loop with funny examples

Example 1: 

while loop is "control flow" statement and it stops executing the block only if the condition fails

The first example shows how to validate on the fly using a while loop. Users want to hit the bell and try and try, and at a certain moment, the system allows them to be admitted. Let us examine the number 10, which satisfies the requirement to be admitted.

bell = int(input('Ring the doorbell:= '))

while True:

    if bell == 10:

        print("ah! Welcome to home!")

        break

    else:

        bell = int(input('Ring the doorbell:= '))





Example 2:

The second example illustrates that the traffic light will change every two seconds and is a very humorous example. "turtle library" was only used to fill in the color and display the circle. how while loop also used a major role in showcasing the three lights Red, Yellow, and Green.

import turtle as ttl
import time

def draw_circle(color):
    # Clear the previous drawing
    ttl.clear()
    # Create a turtle object
    my_turtle = ttl.Turtle()    
    ttl.setup(width=400, height=400)
    # Set fill color
    my_turtle.fillcolor(color)    
    my_turtle.penup()
    my_turtle.goto(0, -180)  
    my_turtle.pendown()
    # Begin filling the circle
    my_turtle.begin_fill()
    # Draw a full circle with a radius of 180 pixels
    my_turtle.circle(180)
   
    my_turtle.end_fill()

# List of colors
l1 = ['red', 'green', 'yellow']

# Iterate through the list
count = 0
while True:
    draw_circle(l1[count])
    time.sleep(2)
    count +=1    
    if count == 3:
        ttl.bye()
        break
ttl.done()





 

Comments