3.02 Data Abstraction
Use lists and dictionaries to group related data and manage complexity.
- Build a collection of levels
- Lesson Plan
- Tech Talk · Start with a collection
- A. Popcorn warm-up · Find the level
- Code Runner Challenge
- Reference Guide
- Code Examples
- C. Apply the abstraction · Total the whole game
- Code Runner Challenge
- Hacks & Practice Tasks
UESL Game Maker · Mission 2 of 2
Build a collection of levels
Grow Star Trail without writing a new variable and calculation for every level.
Classroom prototype: UESL Star Trail, a game in which players collect stars across levels.
UESL’s foundation supports community and opportunities through gaming for people with intellectual and developmental disabilities. Our examples model a game maker’s settings and scores; ask players what helps them enjoy a game.
Lesson Plan
Objective: Represent UESL level data with a list and use it to manage complexity.
Success criteria: Access the intended element, update/append correctly, and explain why the same total calculation works for different lengths.
Tech Talk · Start with a collection
Data abstraction lets a name and operations stand for a collection, without requiring code to manage its storage details. In 3.1, each level had a separate variable. Here, level_stars = [10, 20, 15] represents all levels’ star counts.
A list is ordered, with an element at each index. Python’s first index is 0; College Board pseudocode starts at 1.
A. Popcorn warm-up · Find the level
Predict: What will the three output lines show: the first level’s stars, the second level’s stars, and the number of levels?
Run → change: Run Python, then switch to Pseudocode and compare. Change only the first value from 10 to 14 in either version. Which output changes, and why does the length stay the same?
Runner controls · languages, saving and reset
Each runner offers Python and Pseudocode with equivalent output and a comment explaining every pseudocode line. Switching keeps your edits in each language; it does not translate them. Save keeps the selected code/language after reload; Trash restores the Python starter. College Board's DISPLAY adds a space; OCS shows each call on a new line.
UESL 3.2 Warm-up - Find the level
level_stars = [10, 20, 15]
print(level_stars[0])
print(level_stars[1])
print(len(level_stars))
Code Runner Challenge
UESL 3.2 Warm-up - Find the level
Warm-up debrief · reveal after predicting
Both versions display 10, 20, then 3. Changing the first value to 14 changes only the first output. Replacing a value does not add a level. Python index 1 and AP index 2 both select the second level.
Explain to a partner: Point to one element, its index, and the list’s length. This warm-up is practice; the checkpoint below is the graded Popcorn task.
Reference Guide
Index map · Star Trail’s sample levels
| Level label | Forest | Cavern | Summit |
|---|---|---|---|
| Stars | 10 | 20 | 15 |
| Python index | 0 | 1 | 2 |
| AP index | 1 | 2 | 3 |
For length n, nonnegative Python indices are 0 through n−1; AP indices are 1 through n. Empty lists have no readable element. An invalid AP index terminates the program; Python raises IndexError for an out-of-range index (AP list rules).
Read, replace and grow
| Operation | Python | College Board pseudocode |
|---|---|---|
| Create a collection | level_stars = [10, 20, 15] |
level_stars ← [10, 20, 15] |
| Empty project | level_stars = [] |
level_stars ← [] |
| Read the second level | level_stars[1] |
level_stars[2] |
| Replace its count | level_stars[1] = 25 |
level_stars[2] ← 25 |
| Count levels | len(level_stars) |
LENGTH(level_stars) |
| Add at the end | level_stars.append(12) |
APPEND(level_stars, 12) |
| Insert at position two | level_stars.insert(1, 8) |
INSERT(level_stars, 2, 8) |
| Remove position two | level_stars.pop(1) |
REMOVE(level_stars, 2) |
B. Popcorn checkpoint · Extend the level sequence
Predict: Write the new collection and level count before running. Which AP index selects the same second level as Python index 1?
Run → change: Check both languages. Change the appended value from 5 to 8 in one version and rerun. Explain why updating an element keeps the length, while appending increases it. Record the original prediction, code, output and AP index in your homework’s Popcorn section.
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))
Code Runner Challenge
UESL 3.2 Popcorn - Extend the level sequence
Checkpoint debrief · reveal after predicting
Both versions display [6, 12, 12, 5], then 4. AP index 2 and Python index 1 select the second level.
Changed version: Appending 8 instead gives [6, 12, 12, 8], still four levels. Inserting adds an element at a chosen position and shifts later elements; removing closes the gap and decreases the length by one.
Other representations and copying
Values may repeat and types may mix, such as ["Forest", 10]; our counter keeps every element numeric so addition makes sense. Lists are also called arrays in some languages.
A string is an ordered character sequence: Python "Forest"[0] is "F". Strings cannot be edited character by character; lists can be changed. Copying: Python other = level_stars shares the same list; level_stars.copy() makes a separate shallow copy, sufficient for these numbers. AP other ← level_stars copies the list (Python lists, AP assignment).
Quick partner check: If Python uses other = level_stars, would changing other[0] also affect level_stars? Explain before opening the answer.
Copying debrief
Yes: both names refer to the same Python list. Use other = level_stars.copy() for a separate copy of these numbers. AP other ← level_stars copies the list.
Code Examples
C. Apply the abstraction · Total the whole game
The supplied loop visits each level’s star count. Predict: How many levels and total stars will it display? Run → change: Try [10, 20, 15, 12], then [], changing only the list. Explain why the loop needs no new line for a fourth level.
UESL 3.2 - Total the level collection
level_stars = [10, 20, 15]
total_stars = 0
for stars in level_stars:
total_stars = total_stars + stars
print(len(level_stars))
print(total_stars)
Code Runner Challenge
UESL 3.2 - Total the level collection
View IPYNB Source
# CODE_RUNNER: UESL 3.2 - Total the level collection
level_stars = [10, 20, 15]
total_stars = 0
for stars in level_stars:
total_stars = total_stars + stars
print(len(level_stars))
print(total_stars)
Both versions output: 3, then 45. Read these as level count → total stars. With [10, 20, 15, 12], expect 4, 57; with [], expect 0, 0.
total_stars before every calculation.From College Board: Topic 3.2 connects list/string representations with explaining how abstraction reduces program complexity (AAP-1.C/D, pp. 65–66). Merely storing a list is insufficient evidence: explain which repeated edits it eliminates.
Hacks & Practice Tasks
MCQ Check · Maker decisions
Record A, B or C for each before revealing the key.
- Which AP index selects the first level? A 0 · B 1 · C the list length.
- Replacing one count changes the list length by: A 0 · B 1 · C −1.
- What does
APPEND(level_stars, 5)do? A Change level five · B Add five levels · C Add one element with 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.
MCQ answers and explanations
B, A, C, B. AP starts at 1; replacement keeps the length; APPEND adds one item; the benefit here is simpler maintenance, not a guaranteed speed increase. Record your actual score out of 4.
Homework Hack · Grow your UESL game
- Refactor the 3.1 prototype into
level_stars = [12, 18, 10]. Explain that each element is one level’s collected stars, in level order. - Add
5to the second level and append7for a new level. Use Example C’s traversal to print the collection, level count and total. Expect[12, 23, 10, 7],4,52. - Test the total-only code with
[]: expect0,0. Skip the second-level update when there is no second element. - Explain the edits a list avoids compared with 100 separate variables. Add a design-thinking note: player/maker need, goal, representation considered, prototype and one actual test/revision. Suggest clear level labels so players need not understand indices.
Connection to 3.1: Changing a list still does not recalculate a previously stored total. The list organizes state; assignment and the traversal update it.