我是 Python 的新手,一直在嘗試撰寫二次方程,但我一直遇到這個錯誤:TypeError: bad operand type for unary -: 'str'
def quad_gleichung():
a = input('a:')
b = input('b:')
c = input('c:')
x1 = int(-b (b**2 - (4*a*c))**(0.5)) / (2*a)
x2 = int(-b - (b**2 - (4*a*c))**(0.5)) / (2*a)
print('L?sung 1:', x1)
print('L?sung 2:', x2)
quad_gleichung()
誰能幫幫我嗎?
謝謝!
uj5u.com熱心網友回復:
的回傳型別input是字串。它需要轉換為某種數字型別,float或者int,取決于用例。
因此,將分配更改a, b, c為:
a = int(input("a: ")) # or float(input("a: "))
b = int(input("b: ")) # or float(input("b: "))
c = int(input("c: ")) # or float(input("c: "))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/351936.html
