11
Lesson 11 of 20 · Loops with Variables
Loops with Variables — Part 19
Repeat a different number of times using a variable. This is part 19 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!
Ready?
Loops with Variables — Part 19
You will learn how loops repeat code multiple times and how range() controls how many times.
Full screen — no distractions
🖨️View in Shop
Checkout complete lesson on Loops with Variables for 3rd Grade
Get the full Loops with Variables track as a printable PDF — all lessons, worksheets, and answer keys.
