There is an error trying to sort the list by school number using sort. How do I modify the code?

Asked 2 years ago, Updated 2 years ago, 14 views

class Student():

    def __init__(self, name, hakbun, major, kor, eng, math, score, avg, grade):
        self.name = name
        self.hakbun = hakbun
        self.kor = kor
        self.eng = eng
        self.math = math
        self.score = score
        self.avg = avg
        self.grade = grade

    def print_info(self):
        print("Name: ", self.name)
        print("Self.hakbun")
        print("Korean: ", self.kor)
        print ("English: ", self.eng)
        print ("Math: ", self.math)
        print("Total score: ", self.score)
        print("Average: ", self.avg)
        print ("credit: ", self.grade)

def Insert_student():
    name = input('Enter a name :')
    hakbun = input ('Enter your school number :')
    major = input('Enter department :')
    kor = int (input('Enter your Korean score :')
    eng = int(input('Please enter your English grade :')
    math = int (input('Enter your math score :')
    print("Student entered".\n")

    score = kor + eng + math
    avg = round(score / 3, 3)
    if avg >= 95:
        grade = "A+"
    elif avg >= 90:
        grade = "A0"
    elif avg >= 85:
        grade = "B+"
    elif avg >= 80:
        grade = "B0"
    elif avg >= 75:
        grade = "C+"
    elif avg >= 70:
        grade = "C0"
    elif avg >= 65:
        grade = "D+"
    elif avg >= 60:
        grade = "D0"
    else:
        grade = "F"
    student = Student(name,hakbun, major, kor, eng, math, score, avg, grade)
    return student

def Search_student(student_list, name):
    for i, student in enumerate(student_list):
        if student.name == name:
            print("======================================================================")
            student.print_info()
            print("======================================================================")

        else:
            print('This name does not exist.')

    return student

def delete_contact(student_list, name):
    for i, student in enumerate(student_list):
        if student.name == name:
            del student_list[i]

def print_student(student_list):
    for student in student_list:
        student.print_info()

def print_menu():
    print ("1. Add Data")
    print ("2. Data Retrieval")
    print ("3. Data Delete")
    print ("4. Sort Data")
    print("0.End\n")
    menu = input ("menu selection: ")
    return int(menu)

def main():

    student_list = []
    print()
    print ("Student Care Program \n")
    print("Create Menu\n")
    while 1:
        menu = print_menu()

        # ----- Add Data -----
        if menu == 1:
            student = Insert_student()
            student_list.append(student)

        # ----- Data Retrieval -----
        elif menu == 2:
            name = input ('Please enter the name of the student you want to search for :')
            Search_student(student_list, name)

        # ----- Data Delete -----
        elif menu == 3:
            name = input ('Please enter the name of the student you want to delete :')
            delete_contact(student_list, name)

        # ----- sort data -----
        elif menu == 4:
            sorted_list = sorted (key=lambda hakburn:student.hakburn) <- Error trying to sort using this part.
            print_student(student_list)

        else:
            print("Done".")
            break

if __name__ == "__main__":
    main()

ERROR

Traceback (most recent call last):
  File "C:/Users/test.py", line 119, in <module>
    main()
  File "C:/Users/test.py", line 111, in main
    sorted_list = sorted(sorted_list, key=lambda hakbun:student.hakbun)
UnboundLocalError: local variable 'sorted_list' referenced before assignment

python

2022-09-21 10:21

1 Answers

There's an answer in the comments, but...

# ----- Sort data -----
elif menu == 4:
    sorted_list = sorted(study_list, key=lambda hakburn:student.hakburn) # Error trying to sort using this part.
    print_student(sorted_list)


2022-09-21 10:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.