用于解决二次公式的Python 3程序引发了SyntaxError

时间:2014-03-21 03:31:46

标签: python python-3.x

Python突出显示"+i" elif语句的"discRoot > 0" print语句中的第二个引号。

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()

5 个答案:

答案 0 :(得分:2)

这也解决了复杂根计算中的错误; - )

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()

一样运行
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

答案 1 :(得分:1)

这部分是语法错误:

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

我不能确定你想通过这个来完成什么。也许你的意思是str(root1)+ "i"

您的else子句中也存在语法错误。将其更改为elif

elif discRoot == 0:

或者只使用else

else:

答案 2 :(得分:1)

试试这个:

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

使用','在print语句中分隔字符串和变量,否则使用str函数

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

这是正确的语法:

    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()

输出:

     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

答案 3 :(得分:1)

您的语法错误似乎在这里:

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

当您使用+连接两个项目时,它应该是 out 的引用...您可能希望将该行更改为:

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

或者,你可能想要这个,这取决于你如何解决二次方。

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

答案 4 :(得分:-1)

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()