Computing Real Roots with Decisions

In [3]:
import math
In [ ]:
def quad():
    a, b, c = eval(input("\nPlease 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("\nThe solutions are: ", root1, root2)
In [ ]:
quad()
In [ ]:
def quad2():
    a, b, c = eval(input("\nPlease enter the coefficients (a, b, c): "))
    
    discrim = b**2 - 4 * a * c
    
    if discrim >= 0:
        disc_root = math.sqrt(discrim)
        root1 = (-b + disc_root) / (2 * a)
        root2 = (-b - disc_root) / (2 * a)    
        print("\nThe solutions are: ", root1, root2)
        
    if discrim < 0:
        print("The equation has no real roots!")
In [ ]:
quad2()
In [1]:
def quad3():
    a, b, c = eval(input("\nPlease enter the coefficients (a, b, c): "))
    
    discrim = b**2 - 4 * a * c
    
    if discrim >= 0:
        disc_root = math.sqrt(discrim)
        root1 = (-b + disc_root) / (2 * a)
        root2 = (-b - disc_root) / (2 * a)    
        print("\nThe solutions are: ", root1, root2)
    else:
        print("The equation has no real roots!")
In [4]:
quad3()
Please enter the coefficients (a, b, c): 2,5,2

The solutions are:  -0.5 -2.0
In [5]:
quad3()
Please enter the coefficients (a, b, c): 1.3, 1.6, 3.4
The equation has no real roots!
In [7]:
def quad4():
    a, b, c = eval(input("\nPlease enter the coefficients (a, b, c): "))
    
    discrim = b**2 - 4 * a * c
    
    if discrim < 0:
        print("Equation has no real roots!")
    else:
        if discrim == 0:
            root = -b / (2 * a)
            print("There are double roots at", root)
        else:
            disc_root = math.sqrt(discrim)
            root1 = (-b + disc_root) / (2 * a)
            root2 = (-b - disc_root) / (2 * a)    
            print("\nThe solutions are: ", root1, root2)
In [9]:
quad4()
Please enter the coefficients (a, b, c): 2,5,2

The solutions are:  -0.5 -2.0
In [10]:
def quad5():
    a, b, c = eval(input("\nPlease enter the coefficients (a, b, c): "))
    
    discrim = b**2 - 4 * a * c
    
    if discrim < 0:
        print("Equation has no real roots!")
    elif discrim == 0:
        root = -b / (2 * a)
        print("There are double roots at", root)
    else:
        disc_root = math.sqrt(discrim)
        root1 = (-b + disc_root) / (2 * a)
        root2 = (-b - disc_root) / (2 * a)    
        print("\nThe solutions are: ", root1, root2)
In [11]:
quad5()
Please enter the coefficients (a, b, c): 1.3, 1.6, 3.4
Equation has no real roots!
In [13]:
quad5()
Please enter the coefficients (a, b, c): 1, -2, 1
There are double roots at 1.0
In [ ]: