How do I sort out this "NameError"?

时间:2015-10-06 08:25:15

标签: python-3.x python-3.3 nameerror

def Get_Details():
    Student_Name = input("Enter the name of the student: ")
    Coursework_Mark = int(input("Enter the coursework mark achieved by the student: "))
    while Coursework_Mark < 0 or Coursework_Mark >60:
        print("Try again, remember the coursework mark is out of 60.")
        Coursework_Mark = int(input("Enter the coursework mark achieved by the student: "))

    Prelim_Mark = int(input("Enter the prelim mark achieved by the student: "))
    while Prelim_Mark < 0 or Prelim_Mark > 90:
        print("Try again, remember the prelim mark is out of 90.")
        Prelim_Mark = int(input("Enter the prelim mark achieved by the student: "))

    return Student_Name, Coursework_Mark, Prelim_Mark

def Calculate_Percentage(Coursework_Mark, Prelim_Mark):

    Percentage = ((Coursework_Mark + Prelim_Mark)/150) * 100

    if Percentage >= 70:
        Grade = "A"
    elif 60 >= Percentage <= 69:
        Grade = "B"
    elif 50 >= Percentage <= 59:
        Grade = "C"
    elif 45 >= Percentage <= 50:
        Grade = "D"
    else:
        Grade = "No Award"

    return Percentage, Grade


def Display_Results(Student_Name, Grade):
    print(Student_Name + " achieved a grade " + str(Grade) + ".")

#MAIN PROGRAM
Student_Name, Coursework_Mark, Prelim_Mark = Get_Details()
Percentage = Calculate_Percentage(Coursework_Mark, Prelim_Mark)
Display_Results(Student_Name, Grade)

At the end of the program I receieve:

Program.py", line 41, in <module>
    Display_Results(Student_Name, Grade)
NameError: name 'Grade' is not defined

How can this be fixed? Please help, thank you.

This program asks the user their name, coursework mark (out of 60) and prelim mark (out of 90) and calculates their percentage which is send to their screen as a grade along with their name.

1 个答案:

答案 0 :(得分:0)

函数Calculate_Percentage返回两个值,Percentage和Grade。看起来你想将它们分配给一个单独的变量,就像你在上面一行中使用Get_Details调用中的三个值一样。

所以最后两行应该是这样的:

Percentage, Grade = Calculate_Percentage(Coursework_Mark, Prelim_Mark)
Display_Results(Student_Name, Grade)

请使用Pythonic命名约定使您的代码更具可读性。例如,变量名通常是all_lower_case。