用户应该输入a、B和C的值,然后得到二次方程的根。数学上,我的代码给出了错误的答案:
print "Quadratic Formula Calculator!!!"
a = input("Please, enter value for A: ")
b = input("Please, enter value for B: ")
c = input("Please, enter value for C: ")
quad =(b**2 - 4 * a * c)
if quad >= 0:
quad ** 0.5
print "quad"
else:
print "can not compute"
solution1 = (-b + quad) / (2 * a)
solution2 = (b + quad) / (2 * a)
print " Solution 1!!!:", solution1
print " Soultion 2!!!:", solution2
最佳答案
你需要这个:
quad = quad ** 0.5
而不仅仅是
quad ** 0.5
。解决方案是:
(-b + quad) / (2 * a)
(-b - quad) / (2 * a)
如果你不能计算出鉴别器的负值(你可以,答案是一个复杂的共轭值),只需在
quad >= 0
中移动解的计算和打印。关于python - 二次方程程序输出不正确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14842610/