UESL 3.2 Homework
Popcorn
A. Popcorn Hack 1
Predictions in comments
Code Runner Challenge
UESL 3.2 Homework - grow the level collection
View IPYNB Source
level_stars = [10, 20, 15]
# the following line will output 10. I know this because Python indexing starts at 0, so index 0 is the 1st element of the level_stars list.
print(level_stars[0])
# the following line will output 20. I know this because index 1 is the 2nd element of the level_stars list.
print(level_stars[1])
# the following line will output 3. This is because level_stars has 3 objects and thus is of length 3.
print(len(level_stars))
Actual output: 10, 20, 3
B. Popcorn Hack 2
Prediction: [6, 12, 12, 5] and a level count of 4. AP index 2 selects the same second level as Python index 1.
Run and Change: After changing the append value from 5 to 8, the new list will be [6, 12, 12, 8], with the new object 8 being added and the object at index 1 being changed from 9 to 12. Python (edited version, appending 8):
Code Runner Challenge
UESL 3.2 Test - empty project
View IPYNB Source
level_stars = [6, 9, 12]
level_stars[1] = level_stars[1] + 3
level_stars.append(8)
print(level_stars)
print(len(level_stars))
Pseudocode (edited version):
level_stars ← [6, 9, 12]
level_stars[2] ← level_stars[2] + 3
APPEND(level_stars, 8)
DISPLAY(level_stars)
DISPLAY(LENGTH(level_stars))
Actual output: [6, 12, 12, 8], then 4. Original version (appending 5) gave [6, 12, 12, 5], then 4.
AP index: 2. Updating level_stars[1] just replaces a value that’s already there so the length stays 3, but append adds a new element so it goes up to 4.
MCQ
| MCQ 3.2: 4/4 | answers: B,A,C,B |
- Which AP index selects the first level? A 0 · B 1 · C the list length. B because AP starts at one.
- Replacing one count changes the list length by: A 0 · B 1 · C −1. A because replacing the index doesn’t change the list length.
- What does APPEND(level_stars, 5) do? A Change level five · B Add five levels · C Add one element with value 5. C because append adds an element of value 5.
- Why use a list plus traversal for 100 levels? A It always runs faster · B It avoids a separate variable and sum edit for every level · C It prevents all errors. B - lists compress multiple different variable values into one list.
Homework
Refactored the 3.1 prototype into one list. Each element is the stars collected on one level, in level order (level 1 = 12, level 2 = 18, level 3 = 10). Then I add 5 to the second level, append a new level with 7 stars, and total it with Example C’s loop.
Prediction: [12, 23, 10, 7], 4, 52
# CODE_RUNNER: UESL 3.2 Homework - grow the level collection
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. Matches the prediction.
The if len(level_stars) >= 2 check skips the second-level update when there is no second level, otherwise level_stars[1] would crash on a short list.
Why a list is better than 100 separate variables: with separate variables I’d need level_1_stars through level_100_stars (100 lines) and a total line that adds all 100 by name. Adding level 101 would mean writing a new variable AND editing the total line, and it’s easy to forget one. With a list, all the stars are one line of data, a new level is one append, and the for loop doesn’t change at all because it goes through however many levels there are.
Also like in 3.1, changing the list doesn’t update a total that was already calculated. The loop has to run again (with total_stars reset to 0) to get the new total.
Tests
Language: Python. Each test starts with its own list.
Test 1: Main is the homework cell above: [12, 18, 10] → [12, 23, 10, 7], 4, 52.
Test 2: Empty project, total-only code with []
Prediction: 0, 0
# CODE_RUNNER: UESL 3.2 Test - empty project
level_stars = []
total_stars = 0
for stars in level_stars:
total_stars = total_stars + stars
print(len(level_stars))
print(total_stars)
Actual output: 0, 0
| Test | List | Expected | Actual | Pass |
|---|---|---|---|---|
| Main | [12, 18, 10] | [12, 23, 10, 7], 4, 52 | [12, 23, 10, 7], 4, 52 | Yes |
| Empty | [] | 0, 0 | 0, 0 | Yes |
The same loop works for 4 levels and 0 levels without changing any code. With the empty list the loop just runs 0 times so the total stays at 0.
Design Thinking
| Phase | What I Did |
|---|---|
| Need | A game maker wants to add or reorder levels without losing track of each level’s stars |
| Goal | Keep every level’s stars in one collection, in level order, that’s easy to read, update and add to |
| Representation considered | Separate variables for each level (like 3.1) vs. one list + a for loop. Went with the list |
| Prototype | level_stars = [12, 18, 10] with an update, an append and Example C’s loop for the total |
| Actual test | Ran the main list (got 4 levels, 52 stars) and the empty list (got 0, 0) |
| Revision | Running the update on an empty list would crash with an IndexError since there’s no level_stars[1], so I added if len(level_stars) >= 2 before it |
Suggestion for Better UX
Players and makers shouldn’t have to think in indices. Add level names next to the star counts:
level_names = ["Forest", "Castle", "Mountain", "Sky"]
level_stars = [12, 23, 10, 7]
for i in range(len(level_names)):
print(f"{level_names[i]}: {level_stars[i]} stars")