w3resource

Python Exercise: Find the median of three values

Python Conditional: Exercise - 40 with Solution

Write a Python program to find the median of three values.

Pictorial Presentation:

Python Exercise: Find the median of three values

Sample Solution:

Python Code:

a = float(input("Input first number: "))
b = float(input("Input second number: "))
c = float(input("Input third number: "))
if a > b:
    if a < c:
        median = a
    elif b > c:
        median = b
    else:
        median = c
else:
    if a > c:
        median = a
    elif b < c:
        median = b
    else:
        median = c

print("The median is", median)

Sample Output:

Input first number: 25                                                                                        
Input second number: 55                                                                                       
Input third number: 65                                                                                        
The median is 55.0 

Flowchart :

Flowchart: Find the median of three values

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to display the sign of the Chinese Zodiac for given year in which you were born.
Next: Write a Python program to get next day of a given date.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: Function With Multiple Outputs

If a function is required to return multiple values then:

[Variable] AggregateFunction([Value] for [item] in [collection])