Discuss / Python / 定义一个函数,接收三个参数,返回一元二次方程 #ax^2+bx+c=0的两个解

定义一个函数,接收三个参数,返回一元二次方程 #ax^2+bx+c=0的两个解

Topic source
在此插入代码

定义一个函数,接收三个参数,返回一元二次方程

ax^2+bx+c=0的两个解

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') delta=bb-4ac if delta<0: return x1=(-b+math.sqrt(delta))/(2a) x2=(-b-math.sqrt(delta))/(2*a) if delta==0: return x1 if delta>0: return x1,x2

a=float(input('Please input the a:')) b=float(input('Please input the b:')) c=float(input('Please input the c:')) print(quadratic(a,b,c))


  • 1

Reply