python实现一元二次表达式求值
作者:互联网
import math
import cmath
flag="yes"
while True:
flag= (input("是否计算新方程的根:"))
if (flag=="yes"):
print('a*x*x+b*x+c=0')
a= float (input('a='))
b= float (input('b='))
c= float (input('c='))
p=b*b-4*a*c
if p<0:
pp=str("√"+str(-p))
x1=str((-b)/(2*a))+"+"+str((1/2/a))+pp+"i"
x2=str((-b)/(2*a))+"-"+str((1/2/a))+pp+"i"
print("无实根,两个虚根:"+str(x1),str(x2))
if p>0:
x1 = (-b+math.sqrt(p))/(2*a)
x2 = (-b-math.sqrt(p))/(2*a)
print("两个不相等的实根:"+str(x1),str(x2))
if p==0:
x1 = (-b + math.sqrt(p)) / (2 * a)
print("两个相等的实根:"+str(x1))
else:
break;
标签:实根,python,x2,print,str,求值,input,x1,表达式 来源: https://www.cnblogs.com/yibao/p/14167890.html