Discuss / Python / 交作业

交作业

Topic source

疯羊在田

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

-- coding: utf-8 --

import math

def quadratic(a, b, c): if not isinstance(a, (int, float)): raise TypeError('bad operand type') if not isinstance(b, (int, float)): raise TypeError('bad operand type') if not isinstance(c, (int, float)): raise TypeError('bad operand type') pan = b*2 - 4ac if pan<0: return '此方程无解' elif pan==0: x = -b/(2a) return x elif pan>0: x1 = (-b + math.sqrt(pan))/(2a);x2 = (-b - math.sqrt(pan))/(2a) return x1, x2

print(quadratic(4, 3, 1)) print(quadratic(1, 3, -4))

不知不觉w

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

支持复数解输出

def quadratic(a, b, c): import math delt=b*2 - 4ac if delt >= 0: r1 = (-b + math.sqrt(delt))/(2a) r2 = (-b - math.sqrt(delt))/(2a) else: delt = -delt r1 = complex(-b/(2a) , math.sqrt(delt)/(2a)) r2 = complex(-b/(2a) , -math.sqrt(delt)/(2*a))
return r1, r2


  • 1

Reply