DEV Community

Ari Wira Saputra
Ari Wira Saputra

Posted on

Days_15 All About the Loop

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:
      self.counter +=1
  def methode2(self):
    while self.exit != "yes":
      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: ")
        self.text = input("Are you worng, Do you want to exit: ")

def call():
  testing = looping(1,"","")

Enter fullscreen mode Exit fullscreen mode

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)