You are trying to use 4
as a function:
discriminant = (b ** 2) - 4(a * c)
You missed *
:
discriminant = (b ** 2) - 4 * (a * c)
Also, if your discriminant turns out to be lower than 0, you'll get an unbound local exception:
>>> X_1, X_2 = Quadratic(2, 1, 1)
imaginary
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in Quadratic
UnboundLocalError: local variable 'First_Root' referenced before assignment
You'll need to add a return
there, or better still, raise an exception:
def Quadratic(a, b, c):
discriminant = (b ** 2) - 4(a * c)
if discriminant < 0:
raise ValueError("imaginary")
elif discriminant >= 0:
Sqrt_Disc = Square_Root(discriminant)
First_Root = (-b + Sqrt_Disc) / (2 * a)
Second_Root = (-b - Sqrt_Disc) / (2 * a)
return First_Root, Second_Root
Your Square_Root()
function is missing it's default value for x
:
def Square_Root(n, x=1):
With those changes, your function actually works:
>>> Quadratic(1, 3, -4)
(1, -4)