Discuss / Python / 作业

作业

Topic source

-- coding: utf-8 --

import math

def quadratic(a, b, c): d0 = b b - 4 a c if d0 < 0: x1 = (-b + math.sqrt(-d0)) / (2 a) x2 = (-b - math.sqrt(-d0)) / (2 a) return str(float('%.3f'%x1)) + 'i', str(float('%.3f'%x2)) + 'i' #结果加虚根:str(x1)+'i' elif d0 == 0: x1 = -b / (2 a) x2 = -b / (2 a) return float('%.2f'%x1),float('%.2f'%x2) else: x1 = (-b + math.sqrt(d0)) / (2 a) x2 = (-b - math.sqrt(d0)) / (2 * a) return float('%.2f'%x1),float('%.2f'%x2) print(quadratic(2, 0, 1)) print(quadratic(1, 3, -4))


  • 1

Reply