属性错误还是简化代码?

时间:2017-03-21 13:23:08

标签: python function python-3.x attributes

我正在尝试运行此代码。

class CalcGPA:

    def __init__(self):
        self.g1 = float(input("Please enter grade 1: "))
        self.g2 = float(input("Please enter grade 2: "))
        self.g3 = float(input("Please enter grade 3: "))
        self.g4 = float(input("Please enter grade 4: "))
        self.ap = float(input("Please enter total AP classes: "))
        self.hn = float(input("Please enter total Honors classes: "))
        self.weight = self.ap + (self.hn * 0.5)
        self.grades_list = [self.g1, self.g2, self.g3, self.g4]
        self.gpa_list = []
        self.gpa = 0

def conv_grades(self):
    for i in self.grades_list:
        if i >= 90:
            self.gpa_list.append(4)
        elif 80 <= i <= 89:
            self.gpa_list.append(3)
        elif 70 <= i <= 79:
            self.gpa_list.append(2)
        elif 60 <= i <= 69:
            self.gpa_list.append(1)
        else :
            self.gpa_list.append(0)

    self.gpa = (sum(self.gpa_list) + self.weight) / len(self.gpa_list)

def show_gpa(self):
    print("Your current GPA as calculated is: {}".format(self.gpa))

我从python3开始,然后是import gpaCalc。接下来是,

student_1 = gpaCalc.CalcGPA()

但是,在我输入成绩和类之后再输入

student_1.conv_grades()

我收到此错误

AttributeError: CalcGPA instance has no attribute 'conv_grades'

我可以通过某种方式修复此错误吗?更好的方法是通过输入python3 gpaCalc.py来运行所有这些操作?然后我可以使它非常简单,并能够通过终端在一行中运行它。谢谢!

0 个答案:

没有答案