Discuss / Python / 一元二次源代码共勉

一元二次源代码共勉

Topic source

520bv

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-import math

def quadratic(a, b, c):
    if not (isinstance(a, (int, float))
            and isinstance(b, (int, float))
            and isinstance(c, (int, float))):
        raise TypeError('bad operand type')
    delta = b**2-4*a*c
    if delta < 0:
        print("no result!\n")
    elif delta == 0:
        x1 = b/(-2*a)
        print("x1=x2=%.3f\n" % x1)
    else:
        x1 = (b+math.sqrt(delta)) / 2*a
        x2 = (b-math.sqrt(delta)) / 2*a
        print("x1=%.3f\nx2=%.3f\n" % (x1, x2))
    return

  • 1

Reply