user3444785
user3444785

Reputation: 1

Python 3 program to solve the quadratic formula raises SyntaxError

Python highlights the second quotation mark in the "+i" print statement of the "discRoot > 0" elif statement.

import math
def main():
    print("This program finds the real solutions to a quadratic equation.")
    a,b,c = 0.0,0.0,0.0
    a,b,c = float(input("n\Please enter the coefficients (a, b, c): "))
    discRoot = math.sqrt(b*b-4*a*c)
    if discRoot > 0:
        root1 = (-b + discRoot) / (2*a)
        root2 = (-b - discRoot) / (2*a)
        print("\nThe solutions are: ", root1, root2)
    elif discRoot < 0:
        print("\nThe solutions for this equation are not real.")
        root1 = (-b + (discRoot*-1)) / (2*a)
        root2 = (-b - (discRoot*-1)) / (2*a)
        print("\nThe solutions are: ", root1"+i" , root2"-i")
    else discRoot == 0:
        root1 = (-b + discRoot) / (2*a)
        root2 = (-b - discRoot) / (2*a)
        print("\nThe solutions are: ", root1, root2)



main()

Upvotes: 0

Views: 5626

Answers (5)

sanooj
sanooj

Reputation: 493

try this:

    print "\nThe solutions are: ", root1,"+i " , root2,"-i"

Seperate strings and variables in print statement using ',' or else use str function

    print("\nThe solutions are: ", str(root1)+"+i "+str(root2)+"-i")

This is the correct sysntax:

    import math
    def main():
        print("This program finds the real solutions to a quadratic equation.")
        a,b,c = 0.0,0.0,0.0
        a,b,c = [float(x) for x in (input("\nPlease enter the coefficients (a, b, c): ").split(","))]
        if b*b-4*a*c <0 :  discRoot = math.sqrt(-(b*b-4*a*c)) * -1
        else :  discRoot = math.sqrt(b*b-4*a*c)
        if discRoot > 0:
            root1 = (-b + discRoot) / (2*a)
            root2 = (-b - discRoot) / (2*a)
            print("\nThe solutions are: ", root1, root2)
        elif discRoot < 0:
            print("\nThe solutions for this equation are not real.")
            root1 = (-b + (discRoot*-1)) / (2*a)
            root2 = (-b - (discRoot*-1)) / (2*a)
            print("\nThe solutions are: "+str(root1)+"+i"+str(root2)+"-i")
        elif discRoot == 0: #sdf
            root1 = (-b + discRoot) / (2*a)
            root2 = (-b - discRoot) / (2*a)
            print("\nThe solutions are: ", root1, root2)



    main()

Output:

     python3 a.py                                                                   
     This program finds the real solutions to a quadratic equation.                                   
     Please enter the coefficients (a, b, c):  1.0,2.9,3.0                                           

     The solutions for this equation are not real.

     The solutions are: -0.502635233925+i-2.39736476607-i

Upvotes: 1

Hugh Bothwell
Hugh Bothwell

Reputation: 56654

This fixes the error in your complex root calculation as well ;-)

from math import sqrt

def get_float(prompt):
    while True:
        try:
            return float(input(prompt))
        except ValueError:
            pass

def main():
    print("This program solves a quadratic equation.")

    a = get_float("Value of coefficient A? ")
    b = get_float("Value of coefficient B? ")
    c = get_float("Value of coefficient C? ")

    print("\n{}x**2 + {}x + {} = 0 has ".format(a, b, c), end="")

    discriminant = b*b - 4*a*c
    if discriminant > 0:
        rt = sqrt(discriminant)
        root1 = (-b + rt) / (2 * a)
        root2 = (-b - rt) / (2 * a)
        print("two real solutions: {0:0.4f} and {1:0.4f}".format(root1, root2))
    elif discriminant == 0:
        root = -b / (2 * a)
        print("one real solution: {0:0.4f}".format(root))
    else:
        real = -b / (2 * a)
        # sign is irrelevant because we will +/- anyways;
        #   we want the +ve value for nicer output
        imag = abs(sqrt(-discriminant) / (2 * a))
        print("two complex solutions: {0:0.4f} + {1:0.4f}i and {0:0.4f} - {1:0.4f}i".format(real, imag))

if __name__=="__main__":
    main()

which runs like

This program solves a quadratic equation.
Value of coefficient A? 1
Value of coefficient B? 1
Value of coefficient C? 1

1.0x**2 + 1.0x + 1.0 = 0 has two complex solutions: -0.5000 + 0.8660i and -0.5000 - 0.8660i

Upvotes: 2

user3347570
user3347570

Reputation: 13

def main():
    print("This program finds the real solutions to a quadratic equation.")
    a,b,c = 0.0,0.0,0.0
    a,b,c = float(input("n\Please enter the coefficients (a, b, c): "))
    discRoot = math.sqrt(b*b-4*a*c)
    if discRoot > 0:
        root1 = (-b + discRoot) / (2*a)
        root2 = (-b - discRoot) / (2*a)
        print("\nThe solutions are: ", root1, root2)
    elif discRoot < 0:
        print("\nThe solutions for this equation are not real.")
        root1 = (-b + (discRoot*-1)) / (2*a)
        root2 = (-b - (discRoot*-1)) / (2*a)
        print "\nThe solutions are: ", root1,"+i " , root2,"-i"
    elif discRoot == 0:   #Or use else without any condition
        root1 = (-b + discRoot) / (2*a)
        root2 = (-b - discRoot) / (2*a)
        print("\nThe solutions are: ", root1, root2)
    else :
       continue      



main()

Upvotes: -1

aIKid
aIKid

Reputation: 28292

This part here is a syntax error:

print("\nThe solutions are: ", root1"+i" , root2"-i")

I can't be sure of what you're trying to accomplish by this though. Perhaps you meant str(root1)+ "i"?

Also there's a syntax error in your else clause. Change it to elif:

elif discRoot == 0:

Or just use else:

else:

Upvotes: 1

user3408589
user3408589

Reputation:

Your syntax error seems to be here:

print("\nThe solutions are: ", root1"+i" , root2"-i")

When you use the + to concatenate two items, it should be out of the quotations... You might want to change the line to this:

print("\nThe solutions are: ", str(root1) + "i" , str(root2) + "-i")

Or, you might have wanted this, depending on how you are going about solving the quadratic.

print("\nThe solutions are: ", str(root1) + "+i" , str(root2) + "-i")

Upvotes: 1

Related Questions