编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 

import json

"""数据录入"""
# data = list(range(20))
# print(json.dumps(data), type(json.dumps(data)))

# data_dict = {
#     number: number*number
#     for number in data
#     if number % 2 ==0
# }

# print(json.dumps(data_dict), type(json.dumps(data_dict)))
"""数据读取"""
# info =  """
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
# """
# data = json.loads(info)
# print(data)
# print(type(data))

import os 

file_name = "成绩录入json.txt"
grade_dict = {}
if os.path.exists(file_name):
    with open(file_name, encoding="utf8") as f:
        grade_dict = json.loads(f.read())

    

while True:
    print("*"*10)
    info = input("请输入姓名和成绩:")
    if info == "quit":
        break
    
    fields = info.split()
    if len(fields) != 2:
        continue
    name, grade = fields
    grade = int(grade)
    grade_dict[name] = grade

with open(file_name, "w", encoding="utf8") as f:
    f.write(json.dumps(grade_dict))