13
Lesson 13 of 20 ยท Repeating
Repeating โ Part 20
Do the same step more than once. This is part 20 of 20.
What you'll learn
You will learn how loops repeat code multiple times and how range() controls how many times.
Steps
- 1Look at the 'for' line. It says: repeat the indented code a certain number of times. range(${Math.min(3 + Math.floor(n / 2), 8)}) means ${Math.min(3 + Math.floor(n / 2), 8)} times.
- 2The indented line (print) runs once for each number in range(). So it would print Step 1, Step 2, Step 3... up to Step ${Math.min(3 + Math.floor(n / 2), 8)}.
- 3Without a loop, you'd need to write ${Math.min(3 + Math.floor(n / 2), 8)} separate print() lines! Loops save you from repeating yourself.
- 4The 'i' variable counts which round we're on (starting from 0). That's why we write i + 1 to show Step 1 instead of Step 0.
Key Terms
- Loop
- A way to repeat the same steps multiple times without writing them again and again.
- range()
- Gives you a sequence of numbers (e.g. 0, 1, 2) so the loop knows how many times to run.
- Repeat
- To do the same thing more than once. Loops help us repeat.
Python 3
Output
Loading Python... This may take a few seconds.
Your code runs only in your browser. No code is sent to any server. Stay safe and have fun learning!
๐จ๏ธView in Shop
Checkout complete lesson on Repeating for Kindergarten
Get the full Repeating track as a printable PDF โ all lessons, worksheets, and answer keys.
