Skip to content

script_23.py - Quadratic Equation Classifier

Code

Python
#!/usr/bin/env python3
"""Quadratic equation classifier"""

a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))

if a == 0:
    print("Not a quadratic equation")
else:
    discriminant = b**2 - 4*a*c
    if discriminant > 0:
        print("Two distinct real roots")
    elif discriminant == 0:
        print("One repeated real root")
    else:
        print("Two complex roots")

Explanation

Line 4-6: Variable assignments - Three input() calls converted to float - a, b, c are coefficients in equation ax² + bx + c = 0 - Float type needed for decimal coefficients

Line 8: if a == 0: - Validates that equation is quadratic - Quadratic requires a ≠ 0 (otherwise it's linear or constant) - Guard clause checks error condition first

Line 11: discriminant = b**2 - 4*a*c - ** is exponentiation operator (power) - b**2 means b squared (b × b) - * is multiplication operator - 4*a*c multiplies three values - Order of operations: exponentiation first, then multiplication, then subtraction - discriminant variable stores float result - Formula from quadratic formula: Δ = b² - 4ac

Line 12: if discriminant > 0: - Nested if (inside the else block) - Positive discriminant means two different real number solutions - Mathematical property of quadratic equations

Line 14: elif discriminant == 0: - Zero discriminant means one solution (technically two identical solutions) - Called a "repeated" or "double" root

Line 16: else: - Discriminant must be negative (< 0) - Negative discriminant means no real solutions - Solutions involve imaginary numbers (complex roots)