质量点始终返回0. Python脚本

时间:2017-12-08 06:05:07

标签: python

所以我已经完成了创建GPA计算器的任务,要求我输入一系列课程,课程名称,学分和GPA。除此之外,我应该编写一行代码来获取GPA并将其与该类的学分相乘,以便返回质量点的值。问题是它总是返回零值。任何帮助将不胜感激。

classes = []
grades= []
cr = []
className = ""
num_credits = 0
quality = []
totalQP = []
total = 0
sumtotalQP = 0

Quit = 'N'
print("Welcome to the GPA calculator")
choice = str(input("\nDo you want to calculate your GPA?\nEnter Y to 
calculate or N to exit the program: "))
if choice == 'N':
    exit(0)
num_classes = int(input("How many courses did you take this term?: "))
def collect():
    counter = 0
    i = 0
    x = 0
    y = 0
    z = 0
    sumtotalQP = 0
    while (counter < num_classes) and (z < num_classes) and (y < 
num_classes):
        className = input("Enter name of course: ")
        classes.append(className)
        i = i +1

        num_credits = int(input("Enter the number of credits associated with 
that course: "))
        cr.append(num_credits)
        cr_final = sum(cr)
        z = z +1

        grade = input("Enter Your Grade For This Class(Letter Form): ")
        grade = grade.upper()
        grades.append(grade)
        y = y +1

    for variable in quality:        
        if grade == "A+":
            point = num_credits * 4.0
        elif grade == "A":
            point = num_credits * 4.0
        elif grade == "A-":
            point = num_credits * 3.7
        elif grade == "B+":
            point = num_credits * 3.3
        elif grade == "B":
            point = num_credits * 3.0
        elif grade == "B-":
            point = num_credits * 2.7
        elif grade == "C+":
            point = num_credits * 2.3
        elif grade == "C":
            point = num_credits * 2.0
        elif grade == "C-":
            point = num_credits * 1.7
        elif grade == "D+":
            point = num_credits * 1.3
        elif grade == "D":
            point = num_credits * 1.0
        elif grade == "D-":
            point = num_credits * 0.7
        totalQP.append(point)
        sumtotalQP = sum(totalQP)

    print("Courses: " + str(classes))
    print("Grades: " + str(grades))
    print("Credits: " + str(cr))
    print("Quality Points: " + str(sumtotalQP))
    print()
    print ("Total Earned Credits This Term: " + str(cr_final))
    calculate()
    choice = str(input("\nDo you want to calculate another terms GPA?\nEnter 
Y to calculate or N to exit the program: "))
    if choice == 'Y':
        collect()
    elif choice == 'N':
        print ("You earned", cr_final, "overall and")
        calculate()
        qual_calc()
        exit(0)

def calculate():
    total= 0
    for element in grades:
        if element == "A+":
            total = total + 4.0
        elif element == "A":
            total = total + 4.0
        elif element == "A-":
            total = total + 3.7
        elif element == "B+":
            total = total + 3.3
        elif element == "B":
            total = total + 3.0
        elif element == "B-":
            total = total + 2.7
        elif element == "C+":
            total = total + 2.3
        elif element == "C":
            total = total + 2.0
        elif element == "C-":
            total = total + 1.7
        elif element == "D":
            total = total + 1.0
    gpa = total / num_classes
    print("you earned a GPA of", + gpa)

collect()

1 个答案:

答案 0 :(得分:0)

因为您将num_credits变量定义为全局值。当它试图计算“点”时,它只是顶部并取得num_credits的原始值(零)并因此返回零值。您需要在本地定义num_credits,而不是全局定义。