Roots w/ conditions

We use our our quadratic formula to find two roots of a quadratic equation. When the coefficients a,b,c are given, it is possible they may result in a imaginary number and it causes an exception to occur.

In [7]:
import math

def roots():
    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("The solutions are: {0}, {1}".format(root1, root2))
In [8]:
roots()
Please enter the coefficients ('a,b,c'): 3,2,1
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-3b304e8f2a75> in <module>()
----> 1 roots()

<ipython-input-7-cd587026456b> in roots()
      3 def roots():
      4     a, b, c = eval(input("Please enter the coefficients ('a,b,c'): "))
----> 5     disc_root = math.sqrt(b**2 - 4*a*c)
      6     root1 = (-b + disc_root)/(2*a)
      7     root2 = (-b - disc_root)/(2*a)

ValueError: math domain error

Calculating Roots with two-way condition

We have two mutually-exclusive conditions, here. Either descrim is greater than or equal to zero or it is less than zero. It cannot be both at the same time. So a two-way decision is perfect here. We can then determine if our descrim will result in real roots, or an error.

In [9]:
def roots2():
    a, b, c = eval(input("Please enter the coefficients ('a,b,c'): "))
    
    discrim = b**2 - 4*a*c
    
    # Two-way decision
    if discrim >= 0:
        disc_root = math.sqrt(discrim)
        root1 = (-b + disc_root)/(2*a)
        root2 = (-b - disc_root)/(2*a)
        print("The solutions are: {0}, {1}".format(root1, root2))
    else:  # if discrim < 0
        print("There are no real roots!")
In [10]:
roots2()
Please enter the coefficients ('a,b,c'): 3,2,1
There are no real roots!

Roots w/ Multi-way (if-elif-else)

In [4]:
def roots3():
    a, b, c = eval(input("Please enter the coefficients ('a,b,c'): "))
    
    discrim = b**2 - 4*a*c
    
    # Two-way decision
    if discrim > 0:
        disc_root = math.sqrt(discrim)
        root1 = (-b + disc_root)/(2*a)
        root2 = (-b - disc_root)/(2*a)
        print("The solutions are: {0}, {1}".format(root1, root2))
    elif discrim == 0:
        root = -b / (2 * a)
        print("There is a double root at {0}".format(root))
    else:  # if discrim < 0
        print("There are no real roots!")
In [6]:
roots3()
Please enter the coefficients ('a,b,c'): 4,1,2
There are no real roots!
In [7]:
def roots3a():
    a, b, c = eval(input("Please enter the coefficients ('a,b,c'): "))
    
    discrim = b**2 - 4*a*c
    
    # Multi-way decision
    if discrim < 0:
        print("There are no real roots!")
    elif discrim == 0:
        root = -b / (2 * a)
        print("There is a double root at {0}".format(root))
    else:  # if discrim > 0
        disc_root = math.sqrt(discrim)
        root1 = (-b + disc_root)/(2*a)
        root2 = (-b - disc_root)/(2*a)
        print("The solutions are: {0}, {1}".format(root1, root2))