Python 3.02 - Data Abstractions HW
Python 3.02 Data Abstractions
Completed the Popcorn checkpoint, the MCQ, the level-collection homework, both tests, and the design-thinking note.
Popcorn
Prediction: [6, 12, 12, 5] and 4. Python index 1 is the second level, which is AP index 2. Updating an element keeps the length the same; append() adds one.
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))
Verified output: [6, 12, 12, 5] and 4.
Changing an existing list element does not change the list’s length, but appending adds one new element.
Popcorn Hack Test
This test changes the existing element and appends 8.
Code Runner Challenge
UESL 3.2 Popcorn Test - Append a new level
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Popcorn Test - Append a new level
level_stars = [6, 9, 12]
level_stars[1] = level_stars[1] + 3
level_stars.append(8)
print(level_stars)
print(len(level_stars))
Verified output:
Code Runner Challenge
UESL 3.2 Homework - grow the level collection
View IPYNB Source
[6, 12, 12, 8]
4
MCQ
Answers: B, A, C, B
Score: 4/4
- B — 1: AP lists start at index 1.
- A — 0: replacing a value doesn’t change the length.
- C:
APPENDadds one element with value 5. - B: a list and one loop avoid a variable and a sum edit for every level.
Homework
Each element in level_stars is one level’s stars, in level order.
Verified output: [12, 23, 10, 7], 4, 52.
Code Runner Challenge
UESL 3.2 Test - empty project
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Homework - grow the level collection
level_stars = [12, 18, 10]
# Skip the update if there is no second level.
if len(level_stars) > 1:
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)
Tests
Main test output: [12, 23, 10, 7], 4, 52 (cell above). Empty test output: 0, 0 without crashing.
What the list saves: 100 separate variables would need 100 names and a 100-term sum that has to be edited every time a level is added. With a list, adding a level is one append() and the same loop totals any number of levels.
# CODE_RUNNER: UESL 3.2 Test - empty project
level_stars = []
if len(level_stars) > 1:
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)
Design Thinking
Player/maker need: a game maker wants to add or reorder levels without losing track of each level’s stars.
Goal: keep all the star counts in one ordered list that can grow.
Representation considered: separate variables from 3.1, but they don’t scale past a few levels.
Prototype: level_stars with a bonus update, an append(), and a loop for the total.
Test/revision: without the len() check, accessing level_stars[1] on an empty list would cause an IndexError; the empty test uses the check and prints 0 and 0.
Level labels: show names like “Forest” or “Cavern” so players don’t need to know list indices.