import math class QE: __a = 0 __b = 0 __c = 0 def __init__(self,a,b,c): QE.__a = a QE.__b = b QE.__c = c def geta(self): return QE.__a def getb(self): return QE.__b def getc(self): return QE.__c def getD(self): return QE.__b^2-4*QE.__a*QE.__c def getRoot1(self): M = QE.getD() if M < 0: print("该方程无根!") elif M == 0: print("x1 = x2 = {:.2f}".format( (-QE.__b - math.sqrt(QE.__b ^ 2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a )) else: print("x1 = {:.2f}".format( (-QE.__b - math.sqrt(QE.__b ^ 2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a )) QE.getRoot2() def getRoot2(slef): print("x2 = {:.2f}".format((-QE.__b + math.sqrt(QE.__b ^ 2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a)) a = int(input("请输入a值")) b = int(input("请输入b值")) c = int(input("请输入c值")) B = QE(a,b,c) B.getRoot1()
时间: 2024-04-26 22:22:46 浏览: 11
出现这个错误是因为你在调用 `QE.getD()` 方法时,没有将实例对象作为参数传递给该方法。因为 `getD()` 方法是实例方法,需要使用 `self` 来引用实例对象的属性。你可以将 `QE.getD()` 改为 `self.getD()`,这样就能正确地引用实例对象了。此外,你在 `getRoot2()` 方法中拼写错误,应该是 `self` 而不是 `slef`。下面是修改后的代码:
import math
class QE:
__a = 0
__b = 0
__c = 0
def __init__(self,a,b,c):
QE.__a = a
QE.__b = b
QE.__c = c
def geta(self):
return QE.__a
def getb(self):
return QE.__b
def getc(self):
return QE.__c
def getD(self):
return QE.__b**2-4*QE.__a*QE.__c
def getRoot1(self):
M = self.getD()
if M < 0:
print("该方程无根!")
elif M == 0:
print("x1 = x2 = {:.2f}".format((-QE.__b - math.sqrt(QE.__b**2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a))
else:
print("x1 = {:.2f}".format((-QE.__b - math.sqrt(QE.__b**2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a))
self.getRoot2()
def getRoot2(self):
print("x2 = {:.2f}".format((-QE.__b + math.sqrt(QE.__b**2 - 4 * QE.__a * QE.__c)) / 2 * QE.__a))
a = int(input("请输入a值"))
b = int(input("请输入b值"))
c = int(input("请输入c值"))
B = QE(a,b,c)
B.getRoot1()