UESL 3.2 Homework
Popcorn prediction
Prediction: Changing the appended value from 5 to 8 will output the list [6, 12, 12, 8] and a length of 4. Updating an existing index in place modifies the current value without changing the number of items, whereas appending adds a brand new slot at the end, increasing the total length of the list by 1.
Code Runner Challenge
popcorn C
View IPYNB Source
# CODE_RUNNER: popcorn C
level_stars = [6, 9, 12]
level_stars[1] = level_stars[1] + 3
level_stars.append(8)
print(level_stars)
print(len(level_stars))
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
MCQ
- B
- A
- C
- B
Code Runner Challenge
Homework
View IPYNB Source
# CODE_RUNNER: Homework
# --- Homework Hack · Grow Your UESL Game ---
# 1. Main Game State Run
print("--- Main Game Run ---")
level_stars = [12, 18, 10] # Each element represents one level's collected stars in level order
# Safely add 5 to the second level only if a second level exists
if len(level_stars) >= 2:
level_stars[1] += 5
# Append 7 for a new level
level_stars.append(7)
# Calculate total and level count using a traversal loop
total_stars = 0
for stars in level_stars:
total_stars += stars
level_count = len(level_stars)
# Print results
print("Collection:", level_stars)
print("Level Count:", level_count)
print("Total Stars:", total_stars)
# 2. Edge Case Test: Empty List ([])
print("\n--- Empty List Test Case ---")
level_stars = []
# Skip the second-level update when there is no second element
if len(level_stars) >= 2:
level_stars[1] += 5
total_stars = 0
for stars in level_stars:
total_stars += stars
level_count = len(level_stars)
# Print results for empty state
print("Collection:", level_stars)
print("Level Count:", level_count)
print("Total Stars:", total_stars)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...