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 SolutionClick to expandClick to collapse
Here is the spell to open the portal:
for i in range(5):
print("Open Sesame!")Advanced TipsWant more? Click to expandClick to collapse
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...