# File: quad-c.py # Compute the roots of a quadratic equation (user enters coefficients) # # Matt Bishop, ECS 10, Spring 2014 # 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 try: a = float(input("Enter x2 coefficient: ")) b = float(input("Enter x coefficient: ")) c = float(input("Enter 1 coefficient: ")) except: print("You must enter real numbers for coefficients!!!") return # get the discriminant d = discrim(a, b, c) # compute the roots try: # get the first root r1 = (-b + math.sqrt(d)) / (2*a) # get the second root r2 = (-b - math.sqrt(d)) / (2*a) except: print("The roots of this polynomial are complex numbers") return # print them out print("The roots are", r1, "and", r2) # go! main()