Discuss / Python / Stay Hungry Stay foolish

Stay Hungry Stay foolish

Topic source

longtometosee

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

    if a==0:  

        return '方程不是一元二次方程'

    else:

        g=b**2-4*a*c

        if g<0:

            return '该方程无实数解'

        elif g==0:

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

            return (-b+math.sqrt(g))/(2*a)

        else:

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

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

            return x1,x2

print('quadratic(2,7,6)=',quadratic(2,7,6))

print('quadratic(1,3,5)=',quadratic(1,3,5))


  • 1

Reply