ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python 실습] 성적 관리 프로그램(class 사용)
    Python/Python 실습 2022. 7. 29. 18:07

    Student 클래스를 만들어, 이전에 했던 코드를 고쳐 보았다.

     

    class Student:
        def __init__(self):
            self.std_num = int(input('학번을 입력하세요. : '))
            self.name = input('이름을 입력하세요. : ')
            self.kor = int(input('국어 점수를 입력하세요. : '))
            self.eng = int(input('영어 점수를 입력하세요. : '))
            self.math = int(input('수학 점수를 입력하세요. : '))
            self.total = (self.kor + self.eng + self.math)
            self.avg = self.total / 3
            
        def insert(self, student):
            key = self.std_num
            student[key] = [self.name, self.kor, self.eng, self.math, self.total, self.avg]
            return student
        
        @staticmethod
        def search(student):
            std = int(input('찾고싶은 학생의 학번을 입력하세요. : '))
            for i in student.keys():
                print(str(i), list(zip(subkey,student[i])))
            return student
         
        @staticmethod       
        def update(student):
            upd = int(input('수정하고 싶은 학생의 학번을 입력하세요. : '))
            for key, value in student.items():
                if upd == key:
                    name = input('이름을 입력하세요. : ')
                    kor, eng, math = map(int, input('국어, 영어, 수학 점수를 입력하세요. : ').split())
                    total = kor+eng+math
                    avg = total/3
                    
                    student[upd] = [name, kor, eng, math, total, avg]
            return student
                    
        @staticmethod
        def delete(student):
            dele = int(input('삭제하고 싶은 학생의 학번을 입력하세요. : '))
            for key, value in student.copy().items():
                if dele == key:
                    st = student.pop(dele)
            return student
        
        @staticmethod
        def disp(student):
            for i in student.keys():
                print(str(i), list(zip(subkey,student[i])))
            return student
    import pickle as p
    import os
    
    file = 'C:\\Users\\2019A00298\\Desktop\\python\\students.p'
    student = {}
    
    if os.path.isfile(file):
        if os.path.getsize(file) > 0:
            with open('students.p','rb') as file:
                student = p.load(file)
    
    while True:
        user_input = int(input('1.입력, 2.검색, 3.수정, 4.삭제, 5.출력, 6.종료'))
    
        if user_input == 1:
            st = Student()
            student = st.insert(student)
            
        elif user_input == 2:
            student = Student.search(student)
        
        elif user_input == 3:
            student = Student.update(student)
        
        elif user_input == 4:
            student = Student.delete(student)
        
        elif user_input == 5:
            student = Student.disp(student)
        
        elif user_input == 6:
            print('프로그램을 종료합니다.')
            with open("students.p", 'wb') as file:
                p.dump(student, file)
            break
        
        else:
            print('잘못된 입력입니다.')
            break
Designed by Tistory.