Quadratic Formula

$x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$

In [1]:
import math
In [1]:
def main():
    print("This program finds the real solutions to a quadratic equation")
    print()
    
    a, b, c = eval(input("Please enter the coefficients (a, b, c): "))
    
    disc_root = math.sqrt(b**2 - 4 * a * c)
    root1 = (-b + disc_root)/(2 * a)
    root2 = (-b - disc_root)/(2 * a)
    
    print()
    print("The solutions are:", root1, root2)
In [3]:
main()
This program finds the real solutions to a quadratic

Please enter the coefficients (a, b, c): 3, 4, -1

The solutions are: 0.21525043702153024 -1.5485837703548635
In [4]:
main()
This program finds the real solutions to a quadratic

Please enter the coefficients (a, b, c): 1, 2, 3
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-58ca95c5b364> in <module>()
----> 1 main()

<ipython-input-2-f56748e3ef98> in main()
      5     a, b, c = eval(input("Please enter the coefficients (a, b, c): "))
      6 
----> 7     disc_root = math.sqrt(b**2 - 4 * a * c)
      8     root1 = (-b + disc_root)/(2 * a)
      9     root2 = (-b - disc_root)/(2 * a)

ValueError: math domain error
In [5]:
help(math.sqrt)
Help on built-in function sqrt in module math:

sqrt(...)
    sqrt(x)
    
    Return the square root of x.