为什么没有输入任何数据,会有答案返回呢?
Topic sourceimport math def quadratic(a, b, c): d=b*2-4ac if not isinstance(a, (int, float)) or not isinstance(b, (int, float)) or not isinstance(c, (int, float)): raise TypeError('bad operand type') if a==0 and b !=0: x=-c/b return x elif a==0 and b==0: return "a,b不能同时等于0" elif d>=0: x1=(-b+math.sqrt(d))/(2a) x2=(-b-math.sqrt(d))/(2*a) return "计算结果是:%f,%f"%(x1,x2) elif d<0: return "无解" print(quadratic(2, 3, 1)) # => (-0.5, -1.0) print(quadratic(1, 3, -4)) # => (1.0, -4.0)
- 1
开心Ikos