BugLab002 / 020up to 100
functionsscopereturneasy

The Silent Return

The function find_max should return the largest number in a list.

But it ignores most of the list: find_max([3, 1, 4, 1, 5, 9, 2, 6]) should be 9, and it returns 3. The logic looks right at first glance, but something is wrong about where one of the statements lives.

Fix it without changing the algorithm.

# your fix
+ must use for
- must not call max()
hint 2after hint 1
hint 3after hint 2
solution

Read the code. Before running anything: what does the last line below give?

def find_max(numbers):
max_val = numbers[0]
for n in numbers:
if n > max_val:
max_val = n
return max_val
>>> find_max([1, 5, 3])