Python 3.02: Data Abstractions HW
Python 3.02 Data Abstractions
This notebook covers both Popcorn activities, the graded Homework Hack, tests, and design-thinking explanation.
Popcorn Hack 1: Find the Level
Prediction: the original values print 10, 20, and 3. After changing only the first value from 10 to 14, the first output changes to 14 while the second value and list length stay the same.
Code Runner Challenge
3.2 Popcorn 1 - find the level
View IPYNB Source
# CODE_RUNNER: 3.2 Popcorn 1 - find the level
# A list keeps the level star counts in a specific order.
# Python index 0 selects the first element and index 1 selects the second.
stars_by_level = [14, 20, 15]
print(stars_by_level[0])
print(stars_by_level[1])
print(len(stars_by_level))
Popcorn Hack 2: Extend the Level Sequence
Prediction: [6, 12, 12, 5], length 4.
AP index: AP index 2 selects the same second level as Python index 1. Updating an existing element keeps the length the same, while append() adds a new element and increases the length.
Code Runner Challenge
3.2 Popcorn 2 - update and append
View IPYNB Source
# CODE_RUNNER: 3.2 Popcorn 2 - update and append
# Start with three level scores stored together in one list.
# Update the second element, then append a new level to grow the collection.
stars_by_level = [6, 9, 12]
stars_by_level[1] = stars_by_level[1] + 3
stars_by_level.append(5)
print(stars_by_level)
print(len(stars_by_level))
MCQ
Answers: B, A, C, B
Score: 4/4
Homework Hack: Grow the Level Collection
Each element in stars_by_level represents the stars collected in one level, stored in level order.
Code Runner Challenge
3.2 Homework - update, append and traverse the level collection
View IPYNB Source
# CODE_RUNNER: 3.2 Homework - update, append and traverse the level collection
# Store each level's stars in one ordered list instead of separate variables.
# Guard the second-level update so shorter lists do not cause an index error.
# Traverse the list to calculate the total without hardcoding each element.
stars_by_level = [12, 18, 10]
if len(stars_by_level) > 1:
stars_by_level[1] = stars_by_level[1] + 5
stars_by_level.append(7)
overall_stars = 0
for earned in stars_by_level:
overall_stars += earned
print(stars_by_level)
print(len(stars_by_level))
print(overall_stars)
Tests
Main test produces [12, 23, 10, 7], 4, and 52. The empty-list test produces 0 levels and total 0.
Code Runner Challenge
3.2 Test - empty list total
View IPYNB Source
# CODE_RUNNER: 3.2 Test - empty list total
# Use an empty list to verify the program works when there are no levels yet.
# The length check prevents invalid access to a missing second element.
stars_by_level = []
if len(stars_by_level) > 1:
stars_by_level[1] = stars_by_level[1] + 5
overall_stars = 0
for earned in stars_by_level:
overall_stars += earned
print(len(stars_by_level))
print(overall_stars)
Explanation
A list avoids creating a separate variable for every level and avoids rewriting the total calculation whenever another level is added. The same traversal works whether there are zero levels, four levels, or one hundred levels.
Design Thinking
Player/maker need: A game maker needs to add and reorder levels without maintaining a separate variable for every level.
Goal: Keep all level star totals in one ordered collection that can grow without changing the traversal code.
Representation considered: Separate variables could store the same values, but a list is easier to extend and process as one collection.
Prototype: Store level scores in stars_by_level, update the second level, append a new level, then traverse the list for the total.
Test/revision: The main test produced four levels totaling 52. The empty-list test produced zero levels and a total of 0, so the second-level update is guarded by a length check. Clear level labels can also help players understand the levels without needing to know list indices.