此为构造'求解一元二次方程'类:
# -*- coding: utf-8 -*- """ Created on Thu Sep 3 13:48:08 2020 @author: Jeason """ import cmath class qiujie: def __init__(self,a,b,c): self.delta = (b**2) - (4*a*c) def xiao(self): self.x1 = (-b + cmath.sqrt(self.delta))/(2*a) self.x2 = (-b - cmath.sqrt(self.delta))/(2*a) print('无实数解,有两个虚数解:\nx1 = {}\nx2 = {}'.format(self.x1,self.x2)) def deng(self): self.x1 = -b/2*a print('方程仅有一解:{}'.format(self.x1)) def da(self): self.x1 = (-b + self.delta**0.5)/(2*a) self.x2 = (-b - self.delta**0.5)/(2*a) print('方程有两解,\nx1={},\nx2={}'.format(self.x1,self.x2)) js = input('是否开始进行计算:yes/no--:') while js == 'yes': a,b,c = input('请输入三个数字(空格分隔):').split() a = float(a) b = float(b) c = float(c) x = qiujie(a,b,c) if x.delta < 0: x.xiao() elif x.delta == 0: x.deng() else: x.da() js = input('是否还要继续计算:yes/on--:') print('Thanks for your useage')