These questions follow Repetition. A loop is a bargain: describe
one pass precisely and the computer does all of them. The two things
worth getting exactly right are how many passes happen, and which lines
are inside.
Reading
How many lines does this print, and what is the last one?
for day in range(1, 8): print(f"Day {day}")
Trace it. Give the value of total after each pass, then say
what prints.
weights = [6, 11, 7]total = 0for weight in weights: total = total + weightprint(total)
Find the fault. This prints 7 when it should print 24.
weights = [6, 11, 7]for weight in weights: total = 0 total = total + weightprint(total)
How many lines does this print in total?
for week in range(2): for day in range(3): print(f"Week {week}, day {day}")
Writing
Write a while loop that keeps asking How many bins? until the
answer is made only of digits, then prints
Recording 6 bins. Use .isdigit().
Add up every even number from 2 to 20 and print the total. Do it
twice — once with a range that steps by 2, and once with a range
over every number and an if inside.
Why does range(1, 8) give you a week, and range(1, 7) give you
only six days? State the rule in your own words.
Challenge. Keep asking for weights until the user types done,
then report the heaviest — without storing anything in a list.
Answers
Answer 1
Seven lines, ending with Day 7. range(1, 8) starts at 1 and stops
before 8.
Answer 2
total is 6, then 17, then 24, and 24 prints. The print is
outside the loop, so it runs once, after every pass is done. Move it
inside and you get three lines instead of one.
Answer 3
total = 0 is inside the loop, so every pass throws away the running
total and starts again — the printed value is only the last weight.
Move that line above the for:
weights = [6, 11, 7]total = 0for weight in weights: total = total + weightprint(total)
An accumulator has to be created before the loop and used after it.
Answer 4
Six. The inner loop runs completely — three passes — for each of the
outer loop’s two passes, so the count multiplies: 2 × 3. Nested loops
are how you cover a table, a grid, or every day of every week.
Answer 5
answer = ""while not answer.isdigit(): answer = input("How many bins? ")bins = int(answer)print(f"Recording {bins} bins.")
Starting answer at "" matters: the condition is checked before
the first pass, and an empty string is not made of digits, so the
question gets asked at least once.
Answer 6
total = 0for number in range(2, 21, 2): total = total + numberprint(total)
total = 0for number in range(1, 21): if number % 2 == 0: total = total + numberprint(total)
Both print 110. The first says what you want; the second checks
every number and throws half of them away. Prefer the first, but the
second is the pattern you need the moment the test is more
interesting than “is it even”.
Answer 7
range(a, b) includes a and stops before b, so the count is
b - a. range(1, 8) is seven numbers, 1 through 7. Say it as “up
to but not including”, out loud, until it sticks — this is the most
common off-by-one error in the language.
Answer 8
highest = 0entry = ""while entry != "done": entry = input("Weight in kg (or 'done'): ") if entry.isdigit(): weight = int(entry) if weight > highest: highest = weightprint(f"Heaviest: {highest} kg")
Entering 6, 11, 7, then done prints Heaviest: 11 kg. The
.isdigit() check is what lets done pass through the loop without
int() raising a ValueError. Notice the weakness of starting at
0: if every weight could be negative, nothing would ever beat the
start value — the fix is to record the first value seen instead.