Editor

Maximum of Two

Python Basics · Beginner · 30 XP · about 3 min

Write a function called max_of_two(a, b) that returns the larger of two numbers (without using Python's built-in max() function).

Example

max_of_two(5, 3)   # Returns 5
max_of_two(3, 5)   # Returns 5
max_of_two(7, 7)   # Returns 7
max_of_two(-2, -5) # Returns -2

Requirements

  • Do NOT use the built-in max() function
  • If both numbers are equal, return either one
  • Handle negative numbers correctly
def max_of_two(a, b):
# Your code here - don't use max()!
pass
# Test your function
print(max_of_two(5, 3))
print(max_of_two(-2, -5))

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.