Python字典练习题

学生成绩管理系统

'''

# 确定数据以什么数据类型和格式进行存储
students_dict = {
1001: {
"name": "yuan",
"scores": {
"chinese": 100,
"math": 89,
"english": 100,
}
},
1002: {
"name": "rain",
"scores": {
"chinese": 100,
"math": 100,
"english": 100,
}
},
}

while 1:
print('''
1. 查看所有学生成绩
2. 添加一个学生成绩
3. 修改一个学生成绩
4. 删除一个学生成绩
5. 退出程序
''')
choice = input("请输入您的选择:")

if choice == "1":
# 查看所有学生信息
print("*" * 60)
for sid, stu_dic in students_dict.items():
# print(sid,stu_dic)
name = stu_dic.get("name")
chinese = stu_dic.get("scores").get("chinese")
math = stu_dic.get("scores").get("math")
english = stu_dic.get("scores").get("english")

print("学号:%4s 姓名:%4s 语文成绩:%4s 数学成绩%4s 英文成绩:%4s" % (sid, name, chinese, math, english))
print("*" * 60)
elif choice == "2":
while 1:
sid = input("请输入学生学号>>>")
# 判断该学号是否存在
if int(sid) in students_dict: # 该学号已经存在!
print("该学号已经存在!")
else: # # 该学号不存在!
break

name = input("请输入学生姓名>>>")
chinese_score = input("请输入学生语文成绩>>>")
math_score = input("请输入学生数学成绩>>>")
english_score = input("请输入学生英语成绩>>>")

# 构建学生字典
scores_dict = {
"chinese": chinese_score,
"math": math_score,
"english": english_score,
}
stu_dic = {
"name": name,
"scores": scores_dict
}
print("stu_dic", stu_dic)
students_dict[int(sid)] = stu_dic

elif choice == "3":
while 1:
sid = input("请输入学生学号>>>")
# 判断该学号是否存在
if int(sid) in students_dict: # 该学号已经存在!
break
else: # # 该学号不存在!
print("该修改学号不存在!")

chinese_score = input("请输入学生语文成绩>>>")
math_score = input("请输入学生数学成绩>>>")
english_score = input("请输入学生英语成绩>>>")

# 修改学生成绩
scores_dict = {
"chinese": chinese_score,
"math": math_score,
"english": english_score,
}

students_dict.get(int(sid)).update({"scores": scores_dict})
print("修改成功")

elif choice == "4":
while 1:
sid = input("请输入学生学号>>>")
# 判断该学号是否存在
if int(sid) in students_dict: # 该学号已经存在!
break
else: # # 该学号不存在!
print("该修改学号不存在!")

students_dict.pop(int(sid))
print("删除成功")

elif choice == "5":
# 退出程序
break
else:
print("输入有误!")

'''

 

posted @ 2022-03-26 23:29  呼长喜  阅读(486)  评论(0编辑  收藏  举报