Discuss / Python / 一元二次方程计算器

一元二次方程计算器

Topic source

VH立华

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

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

    d=float(b**2-4*a*c)

    if d<0:

        print('此方程式无解')

        return

    x1=(-b+math.sqrt(d))/(2*a)

    x2=(-b-math.sqrt(d))/(2*a)

    if d>=0:

        return x1,x2


  • 1

Reply