Session 6
5 lessons completed
List Length
CompletedAssignment
Complete get_last_index. Return the length of the inventory list minus 1.
Solution
def get_last_index(inventory):
return len(inventory) - 1Concept
len() function, last index = len(list) - 1
How it went
Solved first run, recognised the one-liner approach independently.
Ratings
Key takeaways
- →Simple one-liner patterns often work — read the problem twice before overcomplicating
List Updates
CompletedAssignment
Complete smelt_ore. Check if inventory[1] == 'Iron Ore'. If so, change it to 'Iron Bar'. Return the inventory.
Solution
def smelt_ore(inventory):
if inventory[1] == "Iron Ore":
inventory[1] = "Iron Bar"
return inventoryConcept
Mutating a list by index: list[i] = new_value
How it went
Solved correctly, one small lookup (index reassignment syntax) via search rather than asking — found the pattern and adapted it. Solution was clean and minimal.
Ratings
Key takeaways
- →Assigning to list[i] changes the original — not a copy. Worth remembering when lists get passed between functions
Match Countdown (backfilled)
CompletedAssignment
Write a countdown from 10 to 1 printing '10...', '9...' etc. At 1 print '1...Fight!'
Solution
def countdown_to_start():
for i in range(10, 0, -1):
if i == 1:
print(f"{i}...Fight!")
else:
print(f"{i}...")Concept
range() with negative step, conditional print on final iteration
How it went
Continue Statement (backfilled)
CompletedAssignment
Complete award_enchantments. Use a counter that resets every 3 quests. Only award enchantment when counter reaches 3. Strength = quest_number * 5.
Solution
def award_enchantments(start, end, step):
counter = 0
for quest_number in range(start, end, step):
counter += 1
if counter < 3:
continue
counter = 0
enchantment_strength = quest_number * 5
print(f"Enchantment of strength {enchantment_strength} awarded for completing {quest_number} quests!")Concept
continue statement to skip loop iterations, counter-based pattern
How it went
Key takeaways
- →continue skips to next iteration immediately — useful for avoiding unnecessary work inside loops
XP Points (backfilled)
CompletedAssignment
Complete calculate_experience_points. Each level requires level * 5 XP. Level 1 starts at 0 XP. Return total XP at the given level.
Solution
def calculate_experience_points(level):
total_xp = 0
for i in range(1, level):
total_xp = total_xp + (i * 5)
return total_xpConcept
Accumulator pattern, running total inside a for loop
How it went
Key takeaways
- →Accumulator pattern: a variable updated inside a loop that carries state across iterations