Session 7
3 lessons completed
Find an Item in a List
CompletedAssignment
Complete contains_leather_scraps using no-index syntax. Iterate over items. If you find 'Leather Scraps', set found = True.
Solution
def contains_leather_scraps(items):
found = False
for leather in items:
if leather == "Leather Scraps":
found = True
return foundConcept
for item in list syntax, loop variable naming for readability
How it went
Solved in ~2 minutes after initial hesitation on loop variable naming.
Ratings
Key takeaways
- →Loop variable names can be meaningful: 'for leather in items' reads better than 'for x in items'
Check Character Levels
CompletedAssignment
Complete check_character_levels. Loop over indexes. If old_character_levels[i] < new_character_levels[i], print i.
Solution
def check_character_levels():
old_character_levels = [1, 42, 43, 53, 12, 3, 32, 34, 54, 32, 43]
new_character_levels = [1, 42, 45, 54, 12, 3, 32, 38, 54, 32, 42]
for i in range(0, len(old_character_levels)):
if old_character_levels[i] < new_character_levels[i]:
print(i)Concept
Index-based list comparison, tracking changes between lists
How it went
Solved correctly, understood the index comparison pattern.
Ratings
Key takeaways
- →Pattern recognition: if two lists need comparing element-by-element, track the indices
Find Max
CompletedAssignment
Complete find_max. Start max_so_far = float('-inf'). Compare each number to max_so_far. If larger, replace. Return max_so_far. Return float('-inf') if list is empty.
Solution
def find_max(nums):
max_so_far = float("-inf")
for num in nums:
if max_so_far < num:
max_so_far = num
return max_so_farConcept
Accumulator pattern, float('-inf') as sentinel value
How it went
Struggled with float('-inf') as a starting point. Breakthrough achieved after Socratic questioning. Applied max_so_far < num without being told — chose comparison direction that reads most naturally.
Ratings
Key takeaways
- →The -inf trick: when finding the biggest thing in a set and you don't know what's in it, start at float('-inf') so the first real value always wins