Lesson 3 of 3
Adding up with a loop
A loop can walk through a list: for piece in [5, 10, 20]: takes each value one after the other.
To build a total, start from 0 and add on every turn: total = total + piece.
total = 0
for piece in [1, 2, 3]:
total = total + piece
print(total)🎯 Your mission
Your chest holds three bags of coins: 5, 10 and 20. Create total starting at 0, add each bag with a loop over the list [5, 10, 20], then print total.
PRO lesson
This advanced chapter is part of CodeAge PRO. Unlock it to keep your adventure going.
Discover PRO →