以下二次方程代码有什么问题?

What is wrong with the following quadratic equation code?

我正在尝试编写一个程序,使用二次公式将标准形式的二次方程式转换为因式分解形式,但我在开始计算的部分遇到了错误。好像是我用的花车有问题,不知道为什么,也不知道怎么解决。

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Users\Josef\Documents\Python\standardFactored.py", line 25, in <module>
    rightS = b^2-4*a*c
TypeError: unsupported operand type(s) for ^: 'float' and 'float'

代码如下:

print("This program will convert standard form quadratic equations to "
      "factored form. ax^2+bx+c --> a(x+ )(x+ )")

while True:
    try:
        a = float(raw_input("a = "))
        break
    except:
        print("that is not a valid number")

while True:
    try:
        b = float(raw_input("b = "))
        break
    except:
        print("that is not a valid number")

while True:
    try:
        c = float(raw_input("c = "))
        break
    except:
        print("that is not a valid number")

rightS = b^2-4*a*c
try:
    math.sqrt(rightS)
except:
    ("There is no factored for for this equation")
    quit()

^ 运算符可能没有达到您的预期。它是二进制 XOR,或 eXclusive OR 运算符。 XOR 运算符不适用于浮点数,因此会产生错误。该错误基本上是说它不能对两个浮点数进行操作。对于指数,使用双星号。请参阅 Python 运算符 here.

例如,a 的 b 次方是:

a ** b

在你的情况下,它将是:

rightS = b ** 2 - 4 * a * c