我是否需要單獨對a變量進行判定是否為0?
import math
def quadratic_equation(a, b, c):
? ? if a == 0:
? ? ? ? x1 = b/(-c)
? ? ? ? return x1
? ? else:
? ? ? ? '''
? ? ? ? t = math.sqrt(b*b-4*a*c)
? ? ? ? x1 = (-b + t) / (2 * a)
? ? ? ? x2 = (-b - t) / (2 * a)
? ? ? ? return x1,x2
? ? ? ? '''
? ? ? ? x1 = ((-b)+math.sqrt(b*b-4*a*c))/(2*a)
? ? ? ? x2 = ((-b)-math.sqrt(b*b-4*a*c))/(2*a)
? ? ? ? return x1,x2
? ? ? ??
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
print quadratic_equation(0,2,1)
而且最后如果a= 0得出的結果就是一個-2,不是tuple,是怎么回事?
2019-07-16
我覺得需要判斷一下,可以看下我的代碼:
import math
def Yyec_x(a , b , c):
??? if a != 0:
??????? m = b * b - 4 * a * c
??????? if m >= 0:
??????????? s = math.sqrt(m)
??????????? x1 = (-b + s) / (2 * a)
??????????? x2 = (-b - s) / (2 * a)
??????????? return x1,x2
??????? else:
??????????? return None
??? else:
??????? return None
print Yyec_x(2,3,0)
print Yyec_x(1,-6,5)
print Yyec_x(0,5,8)
print Yyec_x(2,1,1)
2019-07-15
不要 題目已經說了是一元二次方程
2019-07-15
x1=c/(-b)