7. Largest of three
Write a function largest(a, b, c) that takes three numbers and returns the largest of them. Don't use Python's
max(): the point is to compare the numbers yourself, with if and >.
The numbers can be negative or decimals, and two or all three can be equal. When the largest value appears more than once, return that value.
Example 1
a = 3, b = 9, c = 49Example 2
a = -1, b = -7, c = -3-1Example 3
a = 5, b = 8, c = 88Constraints
a,bandcare whole numbers or decimals from -1000 to 1000.
Submit also runs 4 more tests you can't see.
Practises Booleans and Comparisons (lesson 7).
- Keep the biggest number so far in a variable, and start with the first one:
biggest = a. - Compare it with
b:if b > biggest:, thenbiggest = b. Do the same withc, then returnbiggest. - Ties need no extra code: when the largest value appears twice, returning either copy gives the same number.
Solve it without hints for 3 stars, with hints for 2, after seeing the solution for 1.
One way to solve it, once you've had a go.
Solving it after seeing the solution earns 1 star.
Your Submits show here, with their code, so you can go back to one. They stay in this browser.
def largest(a, b, c):
"""Return the largest of three numbers, without max().
a, b, c: int or float
Returns: int or float, the biggest of the three
"""
# Write your code here
pass
Run your code to see how it does on the test cases, or Submit to check every test.