All About Loops
A while loop allows your code to repeat itself based on a condition you set.
It is similar to an if statement in that you ask a question, and as long as the answer is true, the computer will repeatedly run the code.
Example -
counter = 0
while counter < 10:
print(counter)
counter +=1
In this case, print the variable and then add +=1 to that variable. As long as variable is less than 10, the loop will repeat the code.
You have to be really careful that you don't accidentally invoke an infinite loop! This is where the computer will loop code until the end of time. Without a break. Forever.
counter = 0
while counter >10:
print(counter)
counter +=1
In this above case counter will not print anything upto 10 but it will start printing from 11 as there is counter >10 but the while loop will never break because the while loop condition is always fulfilled here.
Code as an example with reason of coding :
What animal do you want?: Cow
A cow goes moo.
Do you want to exit?: no
What animal do you want?: A Lesser Spotted lemur
Ummm...the Lesser Spotter Lemur goes awooga.
Do you want to exit?: yes
you learned how to create a while loop. However, there are a lot of moving parts that can turn the while loop into an accidental infinite loop...and a nightmare.
while True Loop
Introducing the while True loop...
What do you think the above code does?
Remember you can use the big Stop button on the top if your program does not end.
Make it stop
There is a way to stop the loop with the word break. This exits the loop and stops all code at that point. Even if there is more code written after break that is inside the loop.
After break, the computer jumps out of the loop to the next unindented line of code.
while True:
print("This program is running")
goAgain = input("Go again?: ")
if goAgain == "no":
break
print("Aww, I was having a good time 😭")
Run the code below and notice how the loop will continue until break. Here in the above code when while is True it will keep on printing the print function everytime and it will ask through input that "Go again" if it is no then the loop will break and if it is other than "no" then the loop will continue.
A cow goes moo.
Do you want to exit?: no
What animal do you want?: A Lesser Spotted lemur
Ummm...the Lesser Spotter Lemur goes awooga.
Do you want to exit?: yes
exit = "no"
while exit == "no":
animal_sound = input("What animal sound do you want to hear?")
if animal_sound == "cow" or animal_sound == "Cow":
print("🐮 Moo")
elif animal_sound == "pig" or animal_sound == "Pig":
print ("🐷 Oink")
elif animal_sound == "sheep" or animal_sound == "Sheep":
print ("🐑 Baaa")
elif animal_sound == "duck" or animal_sound == "Duck":
print("🦆 Quack")
elif animal_sound == "dog" or animal_sound == "Dog":
print("🐶 Woof")
elif animal_sound == "cat" or animal_sound == "Cat":
print("🐱 Meow")
else:
print("I don't know that animal sound. Try again.")
exit = input("Do you want to exit?: ")
What do you think the above code does?
Remember you can use the big Stop button on the top if your program does not end.
This type of loop only has two conditions: True and False. Make note of the capital "T" and "F".
There is a way to stop the loop with the word break. This exits the loop and stops all code at that point. Even if there is more code written after break that is inside the loop.
After break, the computer jumps out of the loop to the next unindented line of code.
print("This program is running")
goAgain = input("Go again?: ")
if goAgain == "no":
break
print("Aww, I was having a good time 😭")
Lets see another example :-
counter = 0
while True:
answer = int(input("Enter a number: "))
print("Adding it up!")
counter += answer
print("Current total is", counter)
addAnother = input("Add another? ")
if addAnother == "no":
break
print("Bye!")
Lets get into another example :-
Create a "Name the Lyrics" game. Write your favorite song lyrics with a word or two missing. The user has to figure out the correct song lyric in as few attempts as possible. Find the true lyric master among you!print("Fill in the blank lyrics!")
print("(Type in the blank lyrics and see if you are as cool as me.)")
print("")
count = 0
while True:
print("Never going to ______ you up.")
option = input("what will be the blank word:")
count=count+1
if option == "give":
break
print("Total Attempt", count)
No comments:
Post a Comment