如何使用字典和全局创建成绩簿?

时间:2018-03-27 01:58:03

标签: python string dictionary

我应该拥有好的变量名,并为你编写的所有函数提供有意义的文档字符串

我对此感到非常困惑,如果可能的话,我希望有人帮助我解决一些问题。我想,一旦我开始,我就不会有太多问题。我的代码有什么问题?

gradebook = {}

#################
def assignment(kind, grade, weight = None):
    if not weight:
        weight = 1
    global gradebook


    if kind in gradebook:
        gradebook[kind][0] += grade * weight
        gradebook[kind][1] += weight
    else:
        gradebook[kind] = [grade * weight, weight]
##################
def total(proportions):
    average = {}
    for key, value in gradebook.items():
        if key in average:
            average[key] += value[0] / value[1]
        else:
            average[key] = value[0] / value[1]
###########################
    grade = 0
    for key in average.keys():
        if key in proportions:
            grade += average[key] * proportions[key]
    return grade

1 个答案:

答案 0 :(得分:1)

这应该看起来像你可能想要开始的东西。我真的不确定描述的含义。英语水平很恶劣。

class Gradebook(object):

    def __init__(self):
        self.gradebook = {}

    def assignment(self, kind, grade, weight = None):
        # If a weight was not supplied the assignment 
        # says we should assume it was 1.
        if not weight:
            weight = 1

        # Add the new assignment type if not in the dic yet     
        if kind not in self.gradebook:
            self.gradebook.update({kind: [grade*weight, weight]})
        # Update the running total and add to the cumm weight 
        else:
            pass

    def total(self, proportions):
        pass