1 引入math包
2 定义返回的对象
3 判断b*b-4ac的大小
4 计算代码如下
import math
class Result:
result1 = 0
result2 = 0
def __init__(self, r1, r2):
self.result1 = r1
self.result2 = r2
def __return__(self):
return Result(self.result1, self.result2)
def main(a, b, c):
num = b*b-4*a*c
if num < 0:
return 'no result'
elif num == 0:
return Result((-b+math.sqrt(b*b-4*a*c))/2*a, (-b+math.sqrt(b*b-4*a*c))/2*a)
else:
return Result((-b+math.sqrt(b*b-4*a*c))/2*a, (-b-math.sqrt(b*b-4*a*c))/2*a)
if __name__ == '__main__':
result = main(1, 2, 1)
print result.result1, result.result2