Skip to content

script_17.py - Maximum of Three Numbers

Code

Python
#!/usr/bin/env python3
"""Maximum of three numbers"""

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))

if a >= b and a >= c:
    print(f"Maximum is: {a}")
elif b >= a and b >= c:
    print(f"Maximum is: {b}")
else:
    print(f"Maximum is: {c}")

Explanation

Line 4-6: Variable assignments - Three separate input() calls - Each converted to float type - a, b, c variables store float values - Float allows decimal comparisons

Line 8: if a >= b and a >= c: - >= is greater-than-or-equal operator - and requires BOTH conditions to be True - Checks if a is at least as large as both other numbers - Handles ties: if a equals b or c, a is still considered maximum - a >= b and a >= c both must be True

Line 10: elif b >= a and b >= c: - Only checked if first condition False (a is not maximum) - Same logic: checks if b is greater than or equal to both others - b >= a is redundant (we know a < b from previous condition) but makes code clear

Line 12: else: - Only executes when both previous conditions False - If a is not max and b is not max, then c must be the maximum - Logic: if neither a nor b is largest, c wins by elimination - Handles case where all three are equal (c would be chosen)