Data Abstractions HW
Popcorn
Prediction: The new collection will be [6, 12, 12, 8] and the level count will be 4. 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
# UESL 3.2 Popcorn - Extend the level sequence
level_stars = [6, 9, 12]
level_stars[1] = level_stars[1] + 3
level_stars.append(8) # Changed from 5 to 8
print(level_stars)
print(len(level_stars))
Actual output:
[6, 12, 12, 8]4
Explanation: Updating an element (like level_stars[1]) replaces an existing value, which keeps the list length the same. Appending an element adds a completely new value to the end of the list, which increases the length by one.
MCQ
MCQ 3.2: 4/4 | answers: B,A,C,B
Homework
Code Runner Challenge
UESL 3.2 Homework - Grow your UESL game
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Homework - Grow your UESL game
# Your UESL prototype goes here.
# Each element is one level's collected stars, in level order.
level_stars = [12, 18, 10]
# Skip the second-level update when there is no second element
if len(level_stars) >= 2:
level_stars[1] = level_stars[1] + 5
# Append 7 for a new level
level_stars.append(7)
# Traversal to calculate total
total_stars = 0
for stars in level_stars:
total_stars = total_stars + stars
print(level_stars)
print(len(level_stars))
print(total_stars)
print("--- Empty List Test ---")
# Test the total-only code with []
level_stars_empty = []
if len(level_stars_empty) >= 2:
level_stars_empty[1] = level_stars_empty[1] + 5
total_stars_empty = 0
for stars in level_stars_empty:
total_stars_empty = total_stars_empty + stars
print(level_stars_empty)
print(len(level_stars_empty))
print(total_stars_empty)
Tests
Test 1 (main): starts fresh from level_stars = [12, 18, 10]
- Prediction:
[12, 23, 10, 7],4,52 - Actual output:
[12, 23, 10, 7]452
Test 2 (empty project): starts fresh from level_stars_empty = []
- Prediction:
[],0,0(the second-level update is skipped because there is no second element) - Actual output:
[]00
Complexity Explanation: Using a list plus a traversal loop avoids having to write 100 separate variables (like level_1, level_2) and manually typing out a massive sum calculation (total = level_1 + level_2 + ...). It makes the code much shorter and completely eliminates the need to edit the calculation code every time a new level is added.
Design Thinking
Player/Maker Need: As a game maker, I need a way to easily add or reorder levels without losing track of star counts or rewriting my math formulas. Players need a reliable way to see their total stars across all levels.
Goal: Create one understandable collection in level order that can dynamically update the total score regardless of how many levels exist in the game.
Representation Considered: I considered using 100 separate variables for each level, but decided to use a Python list instead because it can store multiple values under one single name and its length can change dynamically.
Prototype: I built a script that stores level stars in a list ([12, 18, 10]), updates the second level’s score, appends a new level, and loops through the list to calculate the total dynamically.
Actual Test/Revision: I tested the loop with an empty list []. I realized I had to add an if len(level_stars) >= 2: check before adding 5 to the second level, otherwise the empty list test would crash with an IndexError. Also, I suggest implementing clear level labels (like “Forest”, “Cavern”) in the UI so players don’t have to understand Python’s zero-based indexing to know which level they are on.