🐸

The Iteration Spell

Python Basicspython-basics-17-the-iteration-spell
Reward: 80 XP
|

The Time Loop

Hoppy finds himself standing in front of a glowing portal. The inscription reads: "To pass, you must repeat the spell five times."

Doing things over and over again is tiring. In Pythonia, we use Loops to do the hard work for us.

The Iteration Spell

The for loop is the most common way to repeat actions. The range() function generates a sequence of numbers.

for i in range(3):
  print("Knock!")
  print("The door rattles.")

This code will run the indented block 3 times.

Your Mission

1
The Chant

Write a for loop that runs 5 times using range(5).

2
Cast Spell

Inside the loop, print the magic word: "Open Sesame!".

Indentation Matters

Just like with if statements, the code inside the loop must be indented (4 spaces).

Suggested Solution
Click to expand

Here is the spell to open the portal:

for i in range(5):
  print("Open Sesame!")
Advanced Tips
Want more? Click to expand

The range() function is versatile. range(5) gives: 0, 1, 2, 3, 4. It starts at 0 and stops before the number you give it.

pymain.py
Loading...
Terminal
Terminal
Ready to run...