Home > Enterprise >  "FinalGrade is not defined" loop question
"FinalGrade is not defined" loop question

Time:10-23

In my code, I'm trying to display the student with the highest grade in the class after calculating each grade received and their weight. For someone reason, I'm getting that the finalGrade is not defined. How would I display the student with the highest grade and their final grade as well?

def welcome():
    print("Hello and Welcome to Top Students")
    print("Our program lists your students and displays the highest of honors")
    print("Let's get started\n")

def aveGrade():
    quizGrade = int(input("What is the grade this student is receiving for their quiz?\n"))
    partGrade = int(input("What is the grade this student is receiving for their prarticipation?\n"))
    assignGrade = int(input("What is the grade this student is is receiving for their assignment?\n"))
    finalGrade = ((quizGrade * .35)   (partGrade * .15)   (assignGrade * .50))
    print("The final grade for this student is", finalGrade)

def main():
    studentnames = ["Fred", "Daphne", "Velma", "Norville"]
    for student in studentnames:
        print("The current grade standing for", student, "are as follows:\n")
        aveGrade()
    print("The student with the highest final grade is", max(studentnames), "with a total of",max(finalGrade))

CodePudding user response:

Variables only exist in scopes. In this case finalGrades is defined in the aveGrade() function and can only be accessed from within this function. Therefore your main function does not "know" about the finalGrade variable. You can declare finalGrade in the global scope and then asign the value to it in the function.

But the better solution would be to return the value from the function like this:

def aveGrade():
    quizGrade = int(input("What is the grade this student is receiving for their quiz?\n"))
    partGrade = int(input("What is the grade this student is receiving for their prarticipation?\n"))
    assignGrade = int(input("What is the grade this student is is receiving for their assignment?\n"))
    finalGrade = ((quizGrade * .35)   (partGrade * .15)   (assignGrade * .50))
    print("The final grade for this student is", finalGrade)
    return finalGrade #Changed

And then use it like this:

def main():
    studentnames = ["Fred", "Daphne", "Velma", "Norville"]
    for student in studentnames:
        print("The current grade standing for", student, "are as follows:\n")
        finalGrade = aveGrade() #Changed
    print("The student with the highest final grade is", max(studentnames), "with a total of",max(finalGrade))

Addition to the comment:

Velma remains your highest student because your students and their final grade are not connected in any way. Currently you are just calculating and returning the final grade but the program does not know that it belongs to a student. max(studentnames) will always return Velma because in alphabetical order Velma is the highest value. Also max(finalGrade) won't do much because in each iteration of your for loop finalGrade is getting overwritten by the return value of aveGrade() and therefore isn't a list where a max value can be determined. One approach is to save the student and their final grade as a key value pair in a dictionairy

def main():
    studentnames = ["Fred", "Daphne", "Velma", "Norville"]
    studentGradesDic = {} #Added
    for student in studentnames:
        print("The current grade standing for", student, "are as follows:\n")
        studentGradesDic[student] = aveGrade() #Changed
    print("The student with the highest final grade is", list(studentGradesDic.keys())[list(studentGradesDic.values()).index(max(studentGradesDic.values()))], "with a total of",max(studentGradesDic.values())) #Changed
  • Related