我想知道是否有人能告诉我为什么我的解决二次方程的python代码不起作用.我已经浏览过它并且没有发现任何错误.
print("This program will solve quadratic equations for you")
print("It uses the system 'ax**2 + bx + c'")
print("a, b and c are all numbers with or without decimal \
points")
print("Firstly, what is the value of a?")
a = float(input("\n\nType in the coefficient of x squared"))
b = float(input("\n\nNow for b. Type in the coefficient of x"))
c = float(input("\n\nGreat. now what is the c value? The number alone?"))
print("The first value for x is " ,(-b+(((b**2)-(4*a* c))* * 0.5)/(2*a)))
print("\n\nThe second value for x is " ,(-b-(((b * * 2)-(4*a*c))** 0.5)/(2*a)))
Run Code Online (Sandbox Code Playgroud)
当a = 1 b = -4且c = -3时,我期待-1和4但得到5.5和0.5
你的麻烦在试图做二次公式的部分:
(-b+(((b**2)-(4*a* c))* * 0.5)/2*a)
Run Code Online (Sandbox Code Playgroud)
问题在于它*具有相同的优先级,/因此您将除以2然后乘以a.你的括号也是关闭的,所以我减少了不必要的,并移动了错误的.简而言之,-b在分裂之前没有与平方根放在一起.你想要的是:
(-b+(b**2-4*a*c)**0.5)/(2*a)
Run Code Online (Sandbox Code Playgroud)
PS为了提问,最好以下列形式询问:
>>> a = 2
>>> b = 1
>>> c = 3
>>> (-b+(((b**2)-(4*a* c))* * 0.5)/2*a)
got blah, expected blam
Run Code Online (Sandbox Code Playgroud)
由于其他打印和输入不是责任(你应该能够很容易地解决).
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |