Python Exercise: Find the Max of three numbers
Python Functions: Exercise-1 with Solution
Write a Python function to find the maximum of three numbers.
Sample Solution:
Python Code:
# Define a function that returns the maximum of two numbers
def max_of_two(x, y):
# Check if x is greater than y
if x > y:
# If x is greater, return x
return x
# If y is greater or equal to x, return y
return y
# Define a function that returns the maximum of three numbers
def max_of_three(x, y, z):
# Call max_of_two function to find the maximum of y and z,
# then compare it with x to find the overall maximum
return max_of_two(x, max_of_two(y, z))
# Print the result of calling max_of_three function with arguments 3, 6, and -5
print(max_of_three(3, 6, -5))
Sample Output:
6
Explanation:
In the exercise above the code defines two functions: "max_of_two()" which finds the maximum between two numbers, and "max_of_three()" which finds the maximum among three numbers by utilizing the "max_of_two()" function. In the final print statement, the maximum is found among the given numbers: 3, 6, and -5.
Pictorial presentation:
Flowchart:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Python-Functions-Exercises Home.
Next: Write a Python function to sum all the numbers in a list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/python-functions-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics