UESL 3.2 Homework
Popcorn
Prediction: the collection becomes [6, 12, 12, 5] and there are 4 levels. AP index 2 selects the same second level as Python index 1.
Code Runner Challenge
UESL 3.2 Popcorn - Extend the level sequence
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Popcorn - Extend the level sequence
level_stars = [6, 9, 12]
level_stars[1] = level_stars[1] + 3
level_stars.append(5)
print(level_stars)
print(len(level_stars))
Actual output: [6, 12, 12, 5], then 4.
Changed version: appending 8 instead of 5.
Code Runner Challenge
UESL 3.2 Popcorn - Changed version, append 8
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Popcorn - Changed version, append 8
level_stars = [6, 9, 12]
level_stars[1] = level_stars[1] + 3
level_stars.append(8)
print(level_stars)
print(len(level_stars))
Actual output: [6, 12, 12, 8], then 4.
Why: updating level_stars[1] replaces a value that is already in the list, so the length stays the same. append adds a brand-new element to the end, so the length goes up by one.
MCQ
| # | Question | My answer |
|---|---|---|
| 1 | AP index that selects the first level | B 1 |
| 2 | How replacing one count changes the length | A 0 |
| 3 | What APPEND(level_stars, 5) does |
C adds one element with value 5 |
| 4 | Why use a list plus traversal for 100 levels | B it avoids a separate variable and sum edit for every level |
MCQ 3.2: 4/4 | answers: B,A,C,B
Homework
My 3.1 star counter, refactored into one list. Each element of level_stars is one level’s collected stars, in level order: level one, level two, level three. It adds 5 to the second level, appends 7 for a new fourth level, then uses Example C’s traversal to total the stars.
Prediction: [12, 23, 10, 7], 4, 52
Code Runner Challenge
UESL 3.2 Homework - Grow my game
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Homework - Grow my game
level_stars = [12, 18, 10]
if len(level_stars) >= 2:
level_stars[1] = level_stars[1] + 5
level_stars.append(7)
total_stars = 0
for stars in level_stars:
total_stars = total_stars + stars
print(level_stars)
print(len(level_stars))
print(total_stars)
Actual output: [12, 23, 10, 7], 4, 52
What the list saves me: with separate variables, every new level needs a new variable name and an edit to the line that adds up the total. With 100 levels, that is 100 variables and a sum line with 100 terms, and forgetting one gives a wrong total. With a list, a new level is one append, and the same loop totals any number of levels without being changed.
Tests
| Test | Starting list | Predicted | Actual |
|---|---|---|---|
| Main | [12, 18, 10] |
[12, 23, 10, 7], 4, 52 |
[12, 23, 10, 7], 4, 52 |
| Empty (total-only) | [] |
0, 0 |
0, 0 |
The empty test starts fresh and only runs the total. There is no second level, so the update is skipped.
Code Runner Challenge
UESL 3.2 Tests - Empty level list
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Tests - Empty level list
level_stars = []
if len(level_stars) >= 2:
level_stars[1] = level_stars[1] + 5
total_stars = 0
for stars in level_stars:
total_stars = total_stars + stars
print(len(level_stars))
print(total_stars)
Result: 0, then 0. The loop runs zero times, so total_stars stays at its starting value of 0, and checking the length first keeps level_stars[1] from causing an IndexError.
Design Thinking
- Player and maker need: a maker wants to add levels without rewriting code, and players want to know which level earned which stars.
- Goal: store every level in one collection that grows easily and still totals correctly.
- Representation I considered: separate variables (like 3.1) compared with one list. I chose the list because adding a level no longer means changing the total.
- Prototype: the list, update, append, and traversal above.
- Test: the main test gave
52for four levels, and the empty test gave0,0without an error.
Clear level labels: players should not have to understand indices. A matching list of names shows each level by name instead:
Code Runner Challenge
UESL 3.2 Design - Show levels by name, not index
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Design - Show levels by name, not index
level_names = ["Forest", "Cavern", "Summit", "Comet Cave"]
level_stars = [12, 23, 10, 7]
for i in range(len(level_stars)):
print(level_names[i] + ": " + str(level_stars[i]) + " stars")