Find Median Value

Take three numbers and print the median value (neither maximum nor minimum).

IntermediatePhase 1 - Level 4: Logical Operators & Compound StatementsExample 3 of 10
find-median-value.py
1# Take three numbers
2a = float(input("Enter first number: "))
3b = float(input("Enter second number: "))
4c = float(input("Enter third number: "))
5
6# Find median
7if (a >= b and a <= c) or (a <= b and a >= c):
8 median = a
9elif (b >= a and b <= c) or (b <= a and b >= c):
10 median = b
11else:
12 median = c
13
14print(f"Median: {median}")

Output

Enter first number: 10
Enter second number: 5
Enter third number: 15
Median: 10.0

What's going on

Median is the middle value when sorted.

Key Concepts:

Check if a number is between the other two
Median is neither max nor min
Use OR to check both orderings