用python写一元二次方程_使用Python解一元二次方程!
一元二次方程:ax2 + bx + c = 0
求根公式:x = (-b+√(b2-4ac))/2a
判別式:b2-4ac
def my_math(a, b , c):
if not isinstance(a,(int, float)) and not isinstance(b, (int, float)) and not isinstance(c, (int, float)) :
raise TypeError("輸入的參數類型有誤" + a)
# elif not isinstance(b, (int, float)):
# raise TypeError("輸入的參數類型有誤" + b)
# elif not isinstance(c, (int, float)):
# raise TypeError("輸入的參數類型有誤" + c)
else:
dist = pow(b,2) - (4 * a * c)
print(dist)
if dist == 0:
result = -b / (2 * a)
print("只有一個相同的實數:", result)
if dist < 0:
print("沒有實數")
if dist > 0:
result1 = (-b + math.sqrt(dist)) /2 * a
result2 = (-b - math.sqrt(dist)) / 2 * a
print(result1)
print(result2)
my_math(-6,6,6)
總結
以上是生活随笔為你收集整理的用python写一元二次方程_使用Python解一元二次方程!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab和opencv混编(mex问
- 下一篇: Python自动抢红包,从此再也不会错过