Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 16.15 Review - Loops
Practice tracing
for and
while loops, predicting output, and writing loops for repetition.
Subsection 16.15.1 Part A: How Many Times?
Checkpoint 16.15.1 .
Determine how many times each loop runs.
for i in range(5):
print(i)
count = 0
while count < 3:
print(count)
count += 1
Subsection 16.15.2 Part B: Trace the Loop
Checkpoint 16.15.2 .
total = 0
for num in [2, 4, 6]:
total = total + num
Trace the values of
num and
total at each iteration.
Subsection 16.15.3 Part C: Predict the Output
Checkpoint 16.15.3 .
What is printed by each loop?
for ch in "cat":
print(ch)
x = 1
while x <= 3:
print(x)
x += 1
Subsection 16.15.4 Part D: Spot the Problem
Checkpoint 16.15.4 .
count = 1
while count <= 5:
print(count)
Explain what is wrong with this loop.
Subsection 16.15.5 Part E: Create
Checkpoint 16.15.5 .
Write a
for loop that prints each value in the list
[1, 2, 3, 4]. Write a
while loop that prints the numbers
1 through
5. Then write a loop that counts how many letters are in the string
"hello".
Subsection 16.15.6 Think About ...
Checkpoint 16.15.6 .
When is a
for loop a better choice than a
while loop?
You have attempted
of
activities on this page.