While Loop
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.
# All About the Loop
#
#26 MARCH 2023
class looping():
def __init__(self,counter,exit,text):
self.counter = counter
self.exit = exit
self.text = text
def methode(self):
while self.counter < 10:
print(self.counter)
self.counter +=1
def methode2(self):
while self.exit != "yes":
print("🥳")
self.exit = input("exit?: ")
def methode3(self):
while self.text !="yes":
self.text = input("What animal do you wont? ")
if self.text =="cow":
print("a cow goes moo")
self.text = input("Do you want to exit: ")
elif self.text =="lemur":
print("Lemur goes awooga")
self.text = input("Do you want to exit: ")
else:
self.text = input("Are you worng, Do you want to exit: ")
def call():
testing = looping(1,"","")
testing.methode()
testing.methode2()
testing.methode3()
call()
Infinite Loop
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. ðŸ˜
Fix an infinite loop by adding:
variable +=1
This is just saying "count to 10 by 1 each time." to make the loop end.
Don't forget, if your condition is a > then you might need to -=. This will subtract from the variable instead of adding to it.
Top comments (0)