An algorithm that works on your example is not finished. It is finished when it works on the awkward inputs too.
The edge cases worth checking every single time:
Empty input. One item. All items identical. The answer being first. The answer being last. Negative numbers. Zero.
EXAMPLE
loading editor…
YOUR TURN
Your turn → All three pass. Now try largest([]) and watch it break. That is the edge case this function has not handled.
loading editor…
Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.
✦ The classic wrong fix
A tempting way to write this function is to start with biggest = 0 instead of numbers[0]. It looks safer. It is badly wrong: for an all-negative list it returns 0, a number that was never in the list at all. Starting from the first real item is what makes it correct.
◈ Graded Challenge
Write a function that returns the largest number in a list. It must handle an empty list by returning the string "empty", and it must work correctly when every number is negative.
Name it exactly: def largest(numbers):
3 visible + 4 hidden
loading editor…
Mark this lesson complete
Signed in, your progress follows you to every device.