Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在python 2.7中计算二次方程的代码有什么问题?_Python_Python 2.7_Math_Quadratic - Fatal编程技术网

我在python 2.7中计算二次方程的代码有什么问题?

我在python 2.7中计算二次方程的代码有什么问题?,python,python-2.7,math,quadratic,Python,Python 2.7,Math,Quadratic,这段代码适用于“b”和“c”上的任何数字,这是合理的,但只要“a”不是1,代码就会给出错误的数字,我无法找出它的错误所在。当你写/2*a时,你先除以2,然后再乘以a 您希望改为在语句中按操作顺序写入/2*a。 def quad(a, b, c): solution1 = (-b + ((b**2 - 4 * a * c)**0.5)) / 2 * a solution2 = (-b - ((b**2 - 4 * a * c)**0.5)) / 2 * a return s

这段代码适用于“b”和“c”上的任何数字,这是合理的,但只要“a”不是1,代码就会给出错误的数字,我无法找出它的错误所在。

当你写/2*a时,你先除以2,然后再乘以a


您希望改为在语句中按操作顺序写入/2*a。

def quad(a, b, c):
    solution1 = (-b + ((b**2 - 4 * a * c)**0.5)) / 2 * a
    solution2 = (-b - ((b**2 - 4 * a * c)**0.5)) / 2 * a
    return solution1, solution2

while True:
    print "\nax^2 + bx + c = 0"
    a = input("What does 'a' equal? ")
    b = input("What does 'b' equal? ")
    c = input("What does 'c' equal? ")

    answera, answerb = quad(a, b, c)
    print "(x -", str(answera) + ")(x -", str(answerb) + ") = 0"
    print "x=" + str(answera) + ",x=" + str(answerb)
,首先进行2的除法,然后将结果乘以a。这不是你想要的:你想要2*a的结果除以其余的。通过在分母周围加括号来解决此问题:

solution1 = (-b + ((b**2 - 4 * a * c)**0.5)) / 2 * a

你能提供示例输入工作和失败,当前结果,期望结果和失败的解释吗?谢谢你的帮助,它工作了!谢谢,我从没想过!
solution1 = (-b + ((b**2 - 4 * a * c)**0.5)) / (2 * a)