Simple repetition

A final major building block of programming is the possibility of repeating code, of executing the same instruction more than once. In programming, we call this a "loop".

We will look at the keyword while and see that it's very similar to the if-statement we're already familiar with.

Repeating

Look at the following code:

There are seven lines of code, and the computer runs through them one at a time. Once the final line is executed, the program ends. Therefore, we only get to guess once. But we would like to keep guessing, until we get it right!

We could do it like this instead:

import random

correct_answer = random.randint(1, 10)
guess = 0

while guess != correct_answer:
    guess = int(input("Gissa ett tal mellan 1 och 10: "))

    if guess == correct_answer:
        print("Correct!")
    else:
        print("Wrong :(")

The "while" keyword works almost like "if". Both are followed by a logical condition such as "guess != correct". If the condition is true, any indented lines directly afterwards are executed.

The difference is that, once there are no more indented lines, Python returns back to the while keyword. If the condition still is true, it will execute the indented lines again. And again. And again, until the condition no longer is true.

An infinite loop

It would be easy, but wrong, to write the above code like this:

import random
correct_answer = random.randint(1, 10)
guess = int(input("Guess a number between 1 and 10: "))
while guess != correct_answer:
    if guess == correct_answer:
        print("Correct!")
    else:
        print("Wrong :(")

That's because we are asking the user for their guess only once, in the beginning, but not inside the loop. When Python returns to repeat the lines inside the loop, none of those ask the user for another guess. The program will keep printing "Wrong :(" over and over again forever.

Here is another infinite loop:

import time
while True:
    time.sleep(1)
    print("loop")

The condition here is simply "True". It is literally (and by design) never false. So the loop will continue forever.

A looping turtle

Another way of seeing code be repeated is with our friend the turtle:

import turtle
import time

turtle.color('red')

while True:
    turtle.forward(10)
    turtle.left(10) 
    time.sleep(0.25)

Think about what will happen here, and then run the program yourself!

Read page in other languages

In this video, we use a while loop to build another game that tests our math skills.

  • Repetition: Code that is executed more than once by the program "jumping back" to a previous line.
  • Loop: Another word for repetition in programming. In Python we use the keywords while and for.
  • Infinite loop: Code that repeats forever, a loop that perhaps never ends.
  • If-statement: The keyword "if" followed by a logical condition allows our program to make decisions.
  • Logical condition: A comparison between two values. For example "age > 18" or "name == 'Marcus'".
Difficulty level