# 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, Winter 2018 # import math # load up the math functions # # compute the discriminant # parameters: a, b, c: coefficients # returns: b^2 -4ac # def discrim(x, y, z): y = 17 return y * y - 4 * x * z # # the main routine # def main(): # read in the coefficients a = float(raw_input("Enter x2 coefficient: ")) b = float(raw_input("Enter x coefficient: ")) c = float(raw_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 of ", a, "x2 + ", b, "x + ", c, "are", r1, "and", r2 # go! main()