使用函数查找GPA

时间:2016-03-06 06:40:21

标签: python

这应该是根据获得的成绩和课程学分来计算学期GPA的代码。

必须使用coursePoints(credit, grade)函数来计算GPA。

但是在我的代码中,credit值没有改变,并且发生错误。

classnum=int(input("Enter the num of classes: "))

totalcredit=0
totalgpa=0

def main():
    for i in range(1,classnum+1):
        print("class", i)
        credit=int(input("Enter the credit: "))
        grade=str(input("Enter the grade: "))
        totalgpa+=coursePoints(credit,grade)
        totalcredit+=credit

    totalcourse=classnum 
    semestergpa=totalgpa/totalcredit
    print("Semester summary")
    print("courses taken: ", classnum)
    print("credits taken: ", totalcredit)
    print("GPA points: ", totalgpa)
    print("Semester GPA: ", semestergpa)

def coursePoints(Credit,Grade):
    if Grade == 'A+' or Grade == 'a+':
        return 4*Credit
    elif Grade == 'A' or Grade == 'a':
        return 4*Credit
    elif Grade == 'A-' or Grade == 'a-':
        return 3.67*Credit
    elif Grade == 'B+' or Grade == 'b+':
        return 3.33*Credit
    elif Grade == 'B' or Grade =='b':
        return 3*Credit
    elif Grade == 'B-' or Grade == 'b-':
        return 2.67*Credit
    elif Grade == 'C+' or Grade == 'c+':
        return 2.33*Credit
    elif Grade == 'C' or Grade == 'c':
        return 2*Credit               
    elif Grade == 'C-' or Grade == 'c-':                        
        return 1.67*Credit                
    elif Grade =='D+' or Grade == 'd+':                      
        return 1.33*Credit                
    elif Grade == 'D' or Grade == 'd':                        
        return 1*Credit                      
    elif Grade == 'D-' or Grade == 'd-':                                
        return 0.33*Credit                     
    else:
        return 0

main()

1 个答案:

答案 0 :(得分:0)

您似乎正在返回变量赋值。

return gpa = 4*Credit

您不需要等号,只需执行

return 4*Credit

修复所有返回语句。

此外,已经对您的缩进问题进行了编辑,因此这可能也是一个问题,但看起来已经修复了,为了得到正确的逻辑,请修正您的成绩的大小写,正如我所提到的在评论中。