반응형
dackyy
대기의 연대기
dackyy
전체 방문자
오늘
어제
  • 분류 전체보기 (49)
    • java (7)
    • 코딩테스트 (23)
    • python (10)
    • Network (2)
    • Web (7)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 반복문
  • 시프트 연산자
  • 자료형
  • 조건문
  • java
  • 비교 연산자
  • https
  • 산술 연산자
  • SSL
  • switch
  • 논리 연산자
  • 제어문
  • 배열생성
  • 참조 자료형
  • 비트 연산자
  • 증감 연산자
  • 연산
  • 기본 자료형
  • TLS
  • 배열

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
dackyy

대기의 연대기

[Python] 학생성적관리 프로그램 (클래스, json형식 사용)
python

[Python] 학생성적관리 프로그램 (클래스, json형식 사용)

2022. 7. 29. 15:17
반응형

 

import json
class Student():
    studentCnt = 0
    def __init__(self):
        Student.studentCnt += 1
        self.__name = input("이름 : ")
        self.__kor = int(input("국어 성적 : "))
        self.__eng = int(input("영어 성적 : "))
        self.__math = int(input("수학 성적 : "))

    def inputStudent(self, me, path):
        if not os.path.isfile(path):
            new = dict({1:{"name" : "name"}})
            with open(path, 'w', encoding='utf-8') as file:
                json.dump(new,file, indent="\t", ensure_ascii=False)
                file.close()
            me = 0

        with open(path,'r', encoding='UTF-8') as file:
            data = json.load(file)
            if me == 0:
                stNum = len(data)
            else:
                stNum = len(data) + 1
            print(stNum, "번 학생을 입력하겠습니다")
            data[str(stNum)] = {
                "이름" : self.name, "국어" : self.kor, "영어" : self.eng, "수학" : self.math }

            with open(path, 'w', encoding='utf-8') as file:
                json.dump(data, file, indent="\t", ensure_ascii=False)
                print(len(data) + 1,"번학생 입력이 완료되었습니다")
            file.close()

    @staticmethod
    def dispClass(path):
        with open(path,'r', encoding='UTF-8') as file:
            data = json.load(file)
            for key, value in data.items():
                print ("번호 : ",end='')
                print(key, value)
    @staticmethod
    def change(path):
        num = input("번호를 입력해주세요 : ")
        with open(path, 'r', encoding='UTF-8') as file:
            data = json.load(file)
            print(data.get(num))
            subject = input("어떤 과목을 입력하시겠습니까? : ")
            if subject in data.get(num):
                grade = int(input("성적을 입력해주세요 : "))
                if subject == '수학':
                    data[num].update(국어 = grade)
                if subject == '영어':
                    data[num].update(영어 = grade)
                if subject == '수학':
                    data[num].update(수학 = grade)
                with open(path, 'w', encoding='utf-8') as file:
                    json.dump(data, file, indent="\t", ensure_ascii=False)
            else:
                print("없는 과목입니다")

    @staticmethod
    def search(path):
        num = input("번호를 입력해주세요 : ")
        with open(path,'r', encoding='UTF-8') as file:
            data = json.load(file)
            print(data.get(num))
#%%
import sys
import os.path

file_path = "/Users/dackyy/Desktop/classAdmin/"

while True:
    print("학생 성적 관리 프로그램")
    menu = int(input("1. 입력 2. 출력 3. 검색 4. 수정 5. 종료 :"))
    me = int(input("반을 입력해주세요 : "))
    now_path = (file_path + str(me) + ".json")

    if menu == 1:
        print(me, "반을 입력합니다")
        student = Student()
        student.inputStudent(me, now_path)
    elif menu == 2:
        Student.dispClass(now_path)
    elif menu == 3:
        Student.search(now_path)
    elif menu == 4:
        Student.change(now_path)
    elif menu == 5:
        sys.exit("종료")

 

반응형

'python' 카테고리의 다른 글

[Python] 코딩도장 25~ 35  (0) 2022.07.30
[Python] 성적관리 프로그램 json 파일로 학생정보 저장하기  (0) 2022.07.28
[Python] 코딩도장 심사문제 13~24  (0) 2022.07.27
[Python] 몰입형 코딩도장 3~12 심사문제  (0) 2022.07.27
[Python] 로또 경우의 수 프로그램  (0) 2022.07.26
    'python' 카테고리의 다른 글
    • [Python] 코딩도장 25~ 35
    • [Python] 성적관리 프로그램 json 파일로 학생정보 저장하기
    • [Python] 코딩도장 심사문제 13~24
    • [Python] 몰입형 코딩도장 3~12 심사문제
    dackyy
    dackyy

    티스토리툴바