current position:Home>Python----solve quadratic equation in one variable

Python----solve quadratic equation in one variable

2022-11-08 07:20:06will be open today

No wrapper version

import matha,b,c=eval(input())#tupleif a!=0:d=b*b-4^a^cif d>0:x1=-b/(2*a)+math.sqrt(d)/(2*a)x1 = -b / (2 * a) - math.sqrt(d) / (2 * a)print("x1={},x2={}".format(x1,x2))elif d==0:x1=x2=-b/(2*a)print("x1={},x2={}".format(x1,x2))else:real=-b/(2*a)imag=math. sqrt(-d)x1=complex(real,imag)x2=complex(real,-imag)print("x1={},x2={}".format(x1,x2))else:print("Not a quadratic equation")

Package version

import mathdef root(a,b,c):d=b*b-4*a*cif d>0:x1=-b/(2*a)+math.sqrt(d)/(2*a)x2 = -b / (2 * a) - math.sqrt(d) / (2 * a)elif d==0:x1=x2=-b/(2*a)else:real=round(-b/(2*a),2)#round(x,y): rounding functionimag=round(math.sqrt(-d),2)x1=complex(real,imag)x2=complex(real,-imag)return x1,x2def main():a,b,c=eval(input())x1,x2=root(a,b,c)print("x1={},x2={}".format(x1,x2))main()

copyright notice
author[will be open today],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/312/202211080628358099.html

Random recommended