案例:pychon学生信息管理系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
"""
学生信息管理系统
功能:查看学生数据列表,能修改学生数据,能删除数据,能新增数据,能退出管理系
"""
 
# 学生信息的存储列表
student_list = []
 
"""先到文件中读取数据保存变量student_list中"""
import os
if os.path.isfile("student.txt"):
    # 注意:第一次使用,手动创建这个文件,以后可以通过 os模块提供的操作,就可以判断该文件是否存在
    with open("student.txt", "r") as f:
        content = f.read()
        if content:
            student_list = json.loads(content)
else:
    # 如果文件不存在,则使用w模式创建文件
    with open("student.txt", "w") as f:
        pass
 
while True:
    """1. 显示菜单"""
    print("* " * 15)
    print("*  欢迎来到XX学生信息管理系统。")
    print("*")
    print("*  1. 添加学生信息")
    print("*  2. 查看学生信息")
    print("*  3. 修改学生信息")
    print("*  4. 删除学生信息")
    print("*  5. 退出信息系统")
    print("* " * 15)
    print()
    action = int(input("请输入要进行的操作序号(1·5):"))
    if action == 1:
        """添加"""
        print("请输入要录入系统的学生信息...")
        name   = input("姓名:")
        age    = input("年龄:")
        sex    = input("性别:")
        mobile = input("联系电话:")
        # 把学生信息往student_list进行追加
        student_list.append({
            "name": name,
            "age": age,
            "sex": sex,
            "mobile": mobile,
        })
        print(f"student_list={student_list}")
        # 添加学生数据以后,自动保存一份到文件中
        if len(student_list) > 0:
            content = json.dumps(student_list) # 把列表转换成字符串
            with open("student.txt", "w") as file:
                file.write(content)
 
    elif action == 2:
        """查看"""
        for index, item in enumerate(student_list):
            print(f"序号: {index+1}\t\t姓名: {item['name']}\t\t年龄: {item['age']}\t\t性别: {item['sex']}\t\t联系电话: {item['mobile']}")
            print("- " * 15)
 
    elif action == 3:
        """修改"""
        for index, item in enumerate(student_list):
            print(f"序号: {index+1}\t\t姓名: {item['name']}\t\t年龄: {item['age']}\t\t性别: {item['sex']}\t\t联系电话: {item['mobile']}")
            print("- " * 15)
        num = int(input("请输入要更新的学生信息序号(如果部分信息不修改,可以不填写留空):"))
        name   = input(f"姓名({student_list[num-1]['name']}):")
        age    = input(f"年龄({student_list[num-1]['age']}):")
        sex    = input(f"性别({student_list[num-1]['sex']}):")
        mobile = input(f"联系电话({student_list[num-1]['mobile']}):")
        # 更新数据
        data = student_list[num-1] # data与student_list[num-1]是引用关系,所以data被改动,就表示student_list[num-1]被改动了
        if name:
            data["name"] = name
        if age:
            data["age"] = age
        if sex:
            data["sex"] = sex
        if mobile:
            data["mobile"] = mobile
 
        if len(student_list) > 0:
            content = json.dumps(student_list) # 把列表转换成字符串
            with open("student.txt", "w") as file:
                file.write(content)
 
    elif action == 4:
        for index, item in enumerate(student_list):
            print(f"序号: {index+1}\t\t姓名: {item['name']}\t\t年龄: {item['age']}\t\t性别: {item['sex']}\t\t联系电话: {item['mobile']}")
            print("- " * 15)
        text = input("请输入要删除的学生信息序号(如果要一次性删除多个,序号之间使用空格隔开):")
        if text:
            num_list = text.split(" ")
 
            # 删除成员
            num_list.sort(reverse=True)
            for num in num_list:
                num = int(num)
                student_list.pop(num - 1)
 
            # 删除操作会导致数据减少,最少的情况是没有数据,所以这里不能判断student_list
            content = json.dumps(student_list) # 把列表转换成字符串
            with open("student.txt", "w") as file:
                file.write(content)
 
    else:
        print("成功退出系统中....")
        break
    print()
    print()

  

posted @   于漫  阅读(78)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示