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.
Comments
Post a Comment