Python 3.02: Data Abstractions HW
Popcorn
Checkpoint: extend the level sequence.
Prediction: [6, 12, 12, 5], then 4
Code Runner Challenge
Popcorn - Extend the Level Sequence
View IPYNB Source
# CODE_RUNNER: Popcorn - Extend the Level Sequence
level_stars = [6, 9, 12]
# Python index 1 is the second level. The right side is evaluated first: 9 + 3 = 12.
level_stars[1] = level_stars[1] + 3
# append adds a new element at the end, so the length grows from 3 to 4.
level_stars.append(5)
print(level_stars)
print(len(level_stars))
Actual output:
Code Runner Challenge
Homework - Grow Your UESL Game
View IPYNB Source
[6, 12, 12, 5]
4
AP index: Python index 1 selects the second level, which is AP index 2, because College Board lists start at 1 instead of 0.
Why the length changes only once: Updating level_stars[1] replaces an existing element, so the length stays 3. Appending adds a new element, so the length becomes 4. Changing the appended value from 5 to 8 gives [6, 12, 12, 8], still 4 levels, because only the value changed, not the number of elements.
MCQ
| MCQ 3.2: 4/4 | answers: B, A, C, B |
- B (1): AP pseudocode lists start at index 1, so index 1 is the first level.
- A (0): Replacing a count changes a value, not the number of elements.
- C (add one element with value 5):
APPENDadds one new element to the end of the list. - B (avoids a separate variable and sum edit for every level): One list and one traversal work for any number of levels. It doesn’t guarantee speed or prevent every error.
Homework
Grow your UESL game: the 3.1 star counter refactored into a list. Each element is one level’s collected stars, in level order: level_stars[0] is level one, level_stars[1] is level two, and so on.
Prediction: [12, 23, 10, 7], then 4, then 52
Code Runner Challenge
Test 2 - Empty Project
View IPYNB Source
# CODE_RUNNER: Homework - Grow Your UESL Game
# One list replaces the three separate level variables from 3.1.
level_stars = [12, 18, 10]
# Add a 5-star bonus to the second level. The check skips the update when there is no
# second element, so an empty or one-level project won't raise an IndexError.
if len(level_stars) >= 2:
level_stars[1] = level_stars[1] + 5
# A new level is just one more element; no new variable is needed.
level_stars.append(7)
# Traversal: the same loop totals 3 levels, 4 levels, or none.
# total_stars resets to 0 first so an old total can't leak into this calculation.
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
The same logic in College Board pseudocode (AP indices start at 1, so the second level is index 2):
level_stars ← [12, 18, 10]
IF (LENGTH(level_stars) ≥ 2)
{
level_stars[2] ← level_stars[2] + 5
}
APPEND(level_stars, 7)
total_stars ← 0
FOR EACH stars IN level_stars
{
total_stars ← total_stars + stars
}
DISPLAY(level_stars)
DISPLAY(LENGTH(level_stars))
DISPLAY(total_stars)
Tests
Each test starts fresh with its own initialized list.
Test 1: main collection. The Homework cell above starts from [12, 18, 10]. Prediction: [12, 23, 10, 7], 4, 52. Actual output matched: [12, 23, 10, 7], 4, 52. The bonus changed one value without changing the length, the append added a fourth level, and the loop totaled all four (12 + 23 + 10 + 7 = 52) without any new line of code.
Test 2: empty project. The total-only code with an empty list.
Prediction: 0, then 0
# CODE_RUNNER: Test 2 - Empty Project
level_stars = []
# The loop body never runs for an empty list, so total_stars stays at its starting value of 0.
total_stars = 0
for stars in level_stars:
total_stars = total_stars + stars
print(len(level_stars))
print(total_stars)
Actual output:
0
0
Explanation: An empty list has no elements, so the loop runs zero times and the total stays 0. This is also why the Homework code checks len(level_stars) >= 2 before updating the second level: with an empty list, level_stars[1] would raise an IndexError in Python (or end the program in AP pseudocode).
Complexity explanation: With 100 separate variables, a game maker would need 100 names, a 100-term addition for the total, and edits to that addition every time a level is added, removed, or reordered. With a list, adding a level is one append, and the same three-line traversal totals any number of levels with no changes. The list removes the repeated edits, not just the repeated typing.
Connection to 3.1: Changing the list still doesn’t update a total that was already stored. total_stars only changes when the traversal runs again and reassigns it.
Design Thinking
- Player/maker need: A game maker needs to add and reorder levels without losing track of which stars belong to which level, and players need to see their progress in terms they understand.
- Goal: One collection that holds every level’s stars in order and works whether the game has zero levels or many.
- Representation considered: I compared separate variables (from 3.1) with a list. Separate variables meant editing the total for every new level, so I chose a list plus a traversal.
- Prototype: A star counter that updates one level, appends a new level, and totals the whole collection with one loop.
- Test and revision: Testing the empty project showed that updating
level_stars[1]would fail when there is no second level, so I added a length check before the update. - Clear labels for players: Players shouldn’t have to understand indices. A parallel list like
level_names = ["Forest", "Cavern", "Summit", "Lagoon"]lets the game show “Cavern: 23 stars” instead of “index 1: 23.”