Skip to main content\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\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 6.10 Evaluate Loops-WE5-P1
Subgoals for Evaluating a Loop.
-
Determine Loop Components
Start condition and values
Iteration variable and/or update condition
Termination/final condition
Body that is repeated based on indentation
Trace the loop, writing updated values for every iteration or until you identify the pattern
Subsection 6.10.1 Evaluate Loops-WE5-P1
All of the following problems use the same setup, but they are independent of each other.
high_numbers = [300, 200, 100, 400, 500, 300]
low_numbers = [1, 3, 2, 4, 5, 3]
mixed_numbers = [5, 3, -4, 7, 2]
Exercises Exercises
1.
Q21: What is the output of the following loop?
total = 0
for number in high_numbers:
total = total + number // 100
print(total)
3
6
18
18.0
300
1800
2.
Q22: What is the output of the following loop?
total = 0
for number in high_numbers:
total = total + 100
print(total)
6
18
600
1800
1900
2400
3.
Q23: What is the output of the following loop?
combined = ""
for number in low_numbers:
combined = combined + str(number)
print(combined)
18
"18"
"132453"
6
"354231"
4.
Q24: What is the output of the following loop?
max = 0
for number in low_numbers:
if number > max:
max = number
print(max)
5
4
3
2
1
5.
Q25: What is the output of the following loop?
sum = 0
for number in high_numbers:
sum += number
print(sum / len(high_numbers))
150
400
250
360
300
You have attempted
of
activities on this page.