← Back to course
Code Crew G6+ / Week 04 / Tuesday
2/6
Week 04 Β· Loops

Tuesday

Running totals
// for, while, break and a countdown clock
⏱ about 30 min

Tuesday: Running totals

The supply team needs to know how much fuel the engines burn during a five-minute test. The engines burn 15 units every minute, starting from a full tank of 100.

Rocket grabs a calculator. "Five subtractions," he says. "Easy. But they want a report for every single minute, and they want it every day."

Raven sketches a small table with a row for each minute. "What do you notice?" she asks. "Each row starts from the row before it. The tank keeps going down from where it was."

Nova taps one cell of the table. "Would you like a hint?" she asks. "Make a variable before the loop. Inside the loop, change it a little each time round."

Rocket puts the calculator away and opens IDLE.

Changing a variable inside a loop

A variable made before a loop keeps its value from one round to the next. Each time round, the loop can change it a little. This is how loops build up totals or count things down.

# burn_test.py
fuel = 100
for minute in range(1, 6):
    fuel = fuel - 15
    print(f"Minute {minute}: fuel {fuel}")
print("Test over")
Minute 1: fuel 85
Minute 2: fuel 70
Minute 3: fuel 55
Minute 4: fuel 40
Minute 5: fuel 25
Test over

The accumulator pattern

An accumulator starts at 0 before the loop and adds to itself inside the loop. After the loop it holds the total.

# total.py
total = 0
for day in range(1, 8):
    total = total + day
print("Total:", total)
Predict: what does total.py show? (Add 1 to 7.)
WHY THIS EXERCISEThe loop adds each day number to total. So total ends as the sum of 1 to 7, which is 28.

Looping over a string

A for loop can also go through a string one character at a time. This program counts the letter o:

# count_o.py
message = "Mission Control"
count = 0
for letter in message:
    if letter == "o":
        count = count + 1
print("Letter o appears", count, "times")
count_o.py shows: Letter o appears ____ times
In burn_test.py, what is fuel after minute 3?
WHERE DOES IT GO?
  • Read the question.
  • Tap your answer.
Where must total = 0 go for the total to add up?
At your computer
1. Type burn_test.py, save it and run it.
2. Change the burn to 12 units a minute and the test to 8 minutes. Run it.
3. Type total.py and run it. Was 28 right?
4. Change total.py to add the numbers 1 to 100. Predict roughly, then run it.
5. In count_o.py, change the letter to "s" and run it.
# even_count.py: counts and adds the even numbers from 1 to 10
count = 0
total = 0
for n in range(1, 11):
    if n % 2 == 0:
        count = count + 1
        total = total + n
print("Even numbers:", count)
print("Their total:", total)
PRACTICE: TRACE EVEN_COUNT.PY
  • Read the question.
  • Tap your answer.
Which values of n make the if True?
What does the first print show?
What does the second print show?
Where is the if placed?
Change range(1, 11) to range(1, 21). What is count now?
Change n % 2 == 0 to n % 2 == 1, with range(1, 11). What is total now?
total starts at 0 and the loop adds 5 four times. What is total?
StatementTrue or false?
A counter adds 1 each time something happens.?
An accumulator must start at 1.?
An if inside a loop is checked once for every round.?
print inside the loop would show the total only once.?
WHY THIS EXERCISELines inside the loop run every round, so totals are usually printed after the loop ends.

Running totals and counters power most real programs. Tomorrow you will meet a loop that keeps going as long as a condition stays True.

← Monday