Gabbi - 3 months ago 12

Python Question

So I made a python code that solves for x using the quadratic formula. Everything works out in the end except for the signs. For instance, if you want to factor x^2 + 10x + 25, my code outputs -5, -5 when the answer should be 5, 5.

`def quadratic_formula():`

a = int(input("a = "))

b = int(input("b = "))

c = int(input("c = "))

bsq = b * b

fourac = 4 * a * c

sqrt = (bsq - fourac) ** (.5)

oppb = -b

numerator_add = (oppb) + (sqrt)

numerator_sub = (oppb) - (sqrt)

twoa = 2 * a

addition_answer = (numerator_add) / (twoa)

subtraction_answer = (numerator_sub) / (twoa)

print(addition_answer)

print(subtraction_answer)

Answer

Your solution is fine, let's prove it using sympy:

```
>>> (x**2+10*x+25).subs(x,-5)
0
```

As you can see, -5 is one of the roots while 5

```
>>> (x**2+10*x+25).subs(x,5)
100
```

is not, now... if you expand your 2 roots [-5,-5] like:

```
>>> ((x+5)*(x+5)).expand()
x**2 + 10*x + 25
```

You can see like the result matches.

In fact, you can also confirm the roots are correct displaying the quadratic equation:

I'd strongly recommend you review the concept of The Quadratic Formula and when it's clear just come back to the coding