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

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
dackyy

대기의 연대기

[Python] 성적관리 프로그램 json 파일로 학생정보 저장하기
python

[Python] 성적관리 프로그램 json 파일로 학생정보 저장하기

2022. 7. 28. 20:18
반응형

json 파일 형식을 이용하여 성적관리프로그램을 구현하였다.

위 폴더에 반마다 json 파일을 저장하게 구현했고 아래와 같이 학생의 정보가 저장된다.

json 파일 형식이 가독성과 다루기가 편하여 구현하는데도 시간이 많이 걸리지않았다.

import json
def inputStudent(me, path):
    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, "번 학생을 입력하겠습니다")

        while True:
            name = input("이름 : ")
            kor = int(input("국어 성적 : "))
            eng = int(input("영어 성적 : "))
            math = int(input("수학 성적 : "))
            data[str(stNum)] = {
                "이름" : str(name), "국어" : kor, "영어" : eng, "수학" : math }

            with open(path, 'w', encoding='utf-8') as file:
                json.dump(data, file, indent="\t", ensure_ascii=False)
                print(len(data) + 1,"번학생 입력이 완료되었습니다")
            if input("더 입력하시겠습니까? y/n : ") != 'y':
                break
        file.close()
#%%
def newClass(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()
    inputStudent(0, path)
#%%
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)
#%%
def changeSt(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("없는 과목입니다")
#%% md

#%%
def searchSt(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 = "파일을 저장할 경로"

while True:
    print("학생 성적 관리 프로그램")
    menu = int(input("1. 입력 2. 출력 3. 검색 4. 수정 5. 종료 :"))
    if menu == 1:
        me = int(input("몇반을 입력하시겠습니까? : "))
        now_path = file_path + str(me) + ".json"

        if os.path.isfile(now_path):
            print(me, "반을 입력합니다")
            inputStudent(me, now_path)
        else:
            print(me, "반이 없으므로 생성합니다")
            newClass(now_path)
    elif menu == 2:
        me = int(input("몇반을 출력하시겠습니까? : "))
        now_path = (file_path + str(me) + ".json")
        dispClass(now_path)
    elif menu == 3:
        me = int(input("반을 입력해주세요 : "))
        now_path = (file_path + str(me) + ".json")
        searchSt(now_path)

    elif menu == 4:
        me = int(input("반을 입력해주세요 : "))
        now_path = (file_path + str(me) + ".json")
        changeSt(now_path)
    elif menu == 5:
        sys.exit("종료")
반응형

'python' 카테고리의 다른 글

[Python] 코딩도장 25~ 35  (0) 2022.07.30
[Python] 학생성적관리 프로그램 (클래스, json형식 사용)  (0) 2022.07.29
[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

    티스토리툴바