我的错是什么?我正在计算gpa

时间:2015-12-09 01:29:36

标签: python

def gpa(grades):
    total = 0
    if grades == 'A':
        total += 4
    if grades == 'B':
        total += 3
    if grades == 'C':
        total += 2
    if grades == 'D':
        total += 1
    if grades == 'F':
        total += 0
    for x in grades:
        return points = sum(points)/len(grade)

1 个答案:

答案 0 :(得分:1)

您应该提供更多详细信息,说明您希望代码执行的操作以及无法正常工作的原因。

尽管如此,我认为下面的代码很适合你。

def gpa(grades):
    total = 0
    for grade in grades:
        if grade == 'A':
            total += 4
        if grade == 'B':
            total += 3
        if grade == 'C':
            total += 2
        if grade == 'D':
            total += 1
        if grade == 'F':
            total += 0
    return round(float(total)/float(len(grades)),1)

def main():
    print gpa({'A','B','C','D'})

if __name__ == "__main__": main()
  

输出 2.5