print "ax^2 + bx + c = 0" def quad_discriminating_roots(a, b, c, entier = 1e-5): discriminant = b*b - 4*a*c a,b,c,d =complex(a), complex(B)/>, complex(c), complex(discriminant) root1 = (-b + d**0.5)/2./a root2 = (-b - d**0.5)/2./a x = root1 y = root2 if abs(discriminant) < entier: return "real and equal", abs(x), abs(x) if discriminant > 0: return "real", x.real, y.real return "complex", x, y for coeffs in ((5, 2, 4/3.), (4, 6, -2), (3, 2, 1), (1.0, -10.0E5, 1.0)): print "Roots of: %gX^2 %+gX %+g are" % coeffs print " %s: %s, %s" % quad_discriminating_roots(*coeffs)

Now, I want the answers to be rounded to user specific digits (eg.

"%.4f" % x.

I have tried putting this everywhere, but I cannot get it to work. Any thoughts?

Side question, is there any known way of replacing the imaginary unit "j" with the more traditional "i"?