Discuss / Python / 为什么没有输入任何数据,会有答案返回呢?

为什么没有输入任何数据,会有答案返回呢?

Topic source

开心Ikos

#1 Created at ... [Delete] [Delete and Lock User]
d=b**2-4*a*c
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:  #**表示乘方,后面数字表示几次,如2平方,3立方,依次类推
    x1=(-b+math.sqrt(b**2-4*a*c))/2*a
    x2=(-b-math.sqrt(b**2-4*a*c))/2*a
    return "计算结果是:%f,%f"%(x1,x2)
elif d<0:
    return "无解"

开心Ikos

#2 Created at ... [Delete] [Delete and Lock User]

import 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)

开心Ikos

#3 Created at ... [Delete] [Delete and Lock User]

找了好久终于把问题找到。。。。


  • 1

Reply