# File: quad.py # Compute the roots of a quadratic equation (user enters coefficients) # WARNING: the discriminant better be non-negative and the user better # enter real numbers!!!!! # See quad-c.py for how to do it right # # Matt Bishop, MHI 289I, Fall 2024 # import math # load up the math functions # # compute the discriminant # parameters: a, b, c: coefficients # returns: b^2 -4ac # def discrim(a, b, c): return b * b - 4 * a * c # # the main routine # def main(): # read in the coefficients a = float(input("Enter x2 coefficient: ")) b = float(input("Enter x coefficient: ")) c = float(input("Enter 1 coefficient: ")) # get the discriminant d = discrim(a, b, c) # get the first root r1 = (-b + math.sqrt(d)) / (2*a) # get the second root r2 = (-b - math.sqrt(d)) / (2*a) # print them out print("The roots are", r1, "and", r2) # go! main()