python · level 1 · unit 5 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A does the same job again and again.
Imagine skipping with a rope. You do not write down every single jump. You say "jump 10 times" and off you go. A loop is the same idea: you write the lines once, and Python repeats them for you.
means "do this 3 times". Each time round, lap holds a new number from range: 0, then 1, then 2. Careful: range starts at 0 and stops before the number you give. range(2, 6) counts 2, 3, 4, 5.
The colon and the indented lines matter. Every indented line under the for line is inside the loop and repeats. The first line that is not indented runs once, after the loop has finished.
for lap in range(3):
print("Lap", lap)
print("Finished!") Python counts from 0, so 3 laps are numbered 0, 1 and 2.
guess first
Where does the counting stop?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
for number in range(1, 5):
print(number)
your turn
Your turn. Make a cheer repeat.
Pick how many times to cheer, from 2 to 10. Type your own cheer between the quotes, then press run.
fix it
The frog should go Ribbit, then Splash!, three times over. Fix it.
- Run it and look: Splash! only happens once. Which lines are inside the loop?
- Only indented lines repeat. The Splash! line starts at the left edge, so it runs after the loop.
- Add four spaces before print("Splash!") so it lines up under print("Ribbit").
nailed it
you made the computer repeat itself. :D
- A
forloop repeats its indented lines. range(3)gives 0, 1, 2. It stops before 3.- Lines that are not indented run after the loop.
new words in your collection
- loop
- for
- range
This lesson isn’t saved. A free account remembers every lesson you finish.