Calculating Roots

We naively calculate two roots using the quadratic formula

In [1]:
import math
In [7]:
def main():
    print("This program finds the real solutions to the quadratic formula")
    print()
    
    a, b, c = eval(input("Please enter the coefficients (a,b,c): "))
    # a = float(input("Please enter the coefficient 'a': "))
    # b = float(input("Please enter the coefficient 'b': "))
    # c = float(input("Please enter the coefficient 'c': "))



    disc_root = math.sqrt(b*b - 4*a*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 the 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 the quadratic

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

<ipython-input-2-57fb37bd9d91> in main()
     10 
     11 
---> 12     disc_root = math.sqrt(b*b - 4*a*c)
     13     # disc_root = math.sqrt(b**2 - 4*a*c)
     14 

ValueError: math domain error