这是一个学生成绩管理系统python的代码,如何实现标题的要求
class GradeManageSystem:
def 登录系统(self):
print('''
******欢迎进入成绩管理系统******
********* 0-退出系统 *********
********* 1-录入成绩 *********
********* 2-查询成绩 *********
''')
def 录入成绩(self):
self.grade = input('录入成绩,格式为:学生1-成绩,学生2-成绩...').split(',')
def 查询成绩(self):
print(self.grade)
gms = GradeManageSystem()
while True:
gms.登录系统()
choice = int(input('请选择您的操作?'))
if choice == 0:
print('欢迎下次使用,bey!')
break
elif choice == 1:
gms.录入成绩()
elif choice == 2:
gms.查询成绩()
import json
class GradeManageSystem:
def 登录系统(self):
print('''
******欢迎进入成绩管理系统******
********* 0-退出系统 *********
********* 1-录入成绩 *********
********* 2-查询成绩 *********
''')
def 录入成绩(self):
self.grade = input('录入成绩,格式为:学生1-成绩,学生2-成绩...').split(',')
with open("data.txt","w") as f:
f.write(json.dumps(self.grade))
def 查询成绩(self):
try:
with open("data.txt","r") as f:
d = f.read()
self.grade = json.loads(d)
except IOError:
self.grade = []
print(self.grade)
gms = GradeManageSystem()
while True:
gms.登录系统()
choice = int(input('请选择您的操作?'))
if choice == 0:
print('欢迎下次使用,bey!')
break
elif choice == 1:
gms.录入成绩()
elif choice == 2:
gms.查询成绩()6
python中对文件的存取,一般用with open()方式。代码关键部分有注释说明:
class GradeManageSystem:
def login(self):
print('''
******欢迎进入成绩管理系统******
********* 0-退出系统 *********
********* 1-录入成绩 *********
********* 2-查询成绩 *********
''')
def record(self):
self.grade = input('录入成绩,格式为:学生1-成绩,学生2-成绩...').split(',')
# 构造'学生n,成绩'的形式并以分行保存。
lines = [f'学生{i+1}'+','+self.grade[i] +
'\n' for i in range(len(self.grade))]
with open('grades.txt','w',encoding='utf-8') as f:#存入文档
f.writelines(lines)
def check(self):
inp=input('输入要查询学生名:')
with open('grades.txt','r',encoding='utf-8') as f:#读取文档
result=f.readlines()
d={line.split(',')[0]:line.split(',')[-1] for line in result}#构造字典供查询用
if inp in d:
print(f'查找到学生{inp}的成绩为{d[inp]}')
else:
print('未查找到该学生的成绩')
gms = GradeManageSystem()
while True:
gms.login()
choice = int(input('请选择您的操作?'))
if choice == 0:
print('欢迎下次使用,bey!')
break
elif choice == 1:
gms.record()
elif choice == 2:
gms.check()
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632