jeudi 4 août 2016

When didving by 0, I want it to print given text

def roots4(outfile,a,b,c):
    """Prints the solutions of ax² + bx + c = 0, if a != 0"""
    d = b * b - 4 * a * c
    e = (-c / (b))
    solutions = [str(-b / (2 * a))]
    solutions2 = [str((-b + math.sqrt(d)) / 2.0 / a), str((-b - math.sqrt(d)) / 2.0 / a)]
    xre = str((-b) / (2 * a))
    xim = str((math.sqrt(-d)) / (2 * a))
    solutions3 = [xre + " + " + xim +"i", xre + " - " + xim +"i"]

    if d == 0 and b == 0:
        print "X = All complex numbers."
    if c != 0:
        print "X = No real solutions."
    if a == 0 and b > 0 < c:
        print "There's only one solution: " + solutions
    if a != 0 and d == 0:
        print "There's only one solution: " + solutions 
    if a != 0 and d > 0:
        print "There's two solutions: " + solutions2
    if a != 0 and d < 0:
        print "Solutions are: " + solutions3

I get a "ZeroDivisionError: float division by zero" error because I'm dividing by zero when "b" is "0" from an input file. How can I bypass the error so it can print the desired text? My desired output needs to be the desired print statement when meeting the "if" conditions.

Aucun commentaire:

Enregistrer un commentaire