- 实例要求:
- 1.学生基本信息包括
学号、姓名、性别、成绩; - 2.将学号设置为
主键值,根据学号进行相应的“增、删、查、改”; - 实例分析:
- 封装“
增、删、查、改”功能函数,在main函数中调用这些函数即可; - 1.添加学生基本信息:
def add_stu():
global stu_info
stu_id = int(input("学号:"))
stu_name = input("姓名:")
stu_sex = input("性别:")
stu_score = int(input("成绩:"))
for stu in stu_info:
if stu["stu_id"] == stu_id:
print("您输入的学生已经存在,添加失败!!!")
return False
stu = {
"stu_id": stu_id,
"stu_name": stu_name,
"stu_sex": stu_sex,
"stu_score": stu_score
}
stu_info.append(stu)
print("添加成功!!!")
return True
- 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
def del_stu():
global stu_info
stu_id = int(input("请输入所删除的学生学号:"))
for stu in stu_info:
if stu["stu_id"] == stu_id:
stu_info.remove(stu)
return True
print("您输入的学生学号有误,删除失败!!!")
return False
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
def search_stu():
global stu_info
stu_id = int(input("请输入要查询的学生学号:"))
for stu in stu_info:
if stu["stu_id"] == stu_id:
print("stu_id:", stu["stu_id"])
print("stu_name:", stu["stu_name"])
print("stu_sex:", stu["stu_sex"])
print("stu_score:", stu["stu_score"])
return True
print("您输入的学生学号不存在,查询失败!!!")
return False
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
def modify_stu():
global stu_info
stu_id = int(input("请输入要修改的学生学号:"))
for stu in stu_info:
if stu["stu_id"] == stu_id:
stu["stu_id"] = int(input("新的学号:"))
stu["stu_name"] = input("新的姓名:")
stu["stu_sex"] = input("新的性别:")
stu["stu_score"] = int(input("新的成绩:"))
return True
print("您输入的学生学号不存在,修改失败!!!")
return False
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
from typing import List, Any
stu_info: list[Any] = []
def print_menu():
print("-----------------------------------------------------")
print("-------------欢迎使用学生信息管理系统---------------------")
print("-----------------1.遍历学生信息------------------------")
print("-----------------2.添加学生信息------------------------")
print("-----------------3.删除学生信息------------------------")
print("-----------------4.查询学生信息------------------------")
print("-----------------5.修改学生信息------------------------")
print("-----------------6.退出系统---------------------------")
print("-----------------------------------------------------")
def add_stu():
global stu_info
stu_id = int(input("学号:"))
stu_name = input("姓名:")
stu_sex = input("性别:")
stu_score = int(input("成绩:"))
for stu in stu_info:
if stu["stu_id"] == stu_id:
print("您输入的学生已经存在,添加失败!!!")
return False
stu = {
"stu_id": stu_id,
"stu_name": stu_name,
"stu_sex": stu_sex,
"stu_score": stu_score
}
stu_info.append(stu)
print("添加成功!!!")
return True
def del_stu():
global stu_info
stu_id = int(input("请输入所删除的学生学号:"))
for stu in stu_info:
if stu["stu_id"] == stu_id:
stu_info.remove(stu)
return True
print("您输入的学生学号有误,删除失败!!!")
return False
def search_stu():
global stu_info
stu_id = int(input("请输入要查询的学生学号:"))
for stu in stu_info:
if stu["stu_id"] == stu_id:
print("stu_id:", stu["stu_id"])
print("stu_name:", stu["stu_name"])
print("stu_sex:", stu["stu_sex"])
print("stu_score:", stu["stu_score"])
return True
print("您输入的学生学号不存在,查询失败!!!")
return False
def modify_stu():
global stu_info
stu_id = int(input("请输入要修改的学生学号:"))
for stu in stu_info:
if stu["stu_id"] == stu_id:
stu["stu_id"] = int(input("新的学号:"))
stu["stu_name"] = input("新的姓名:")
stu["stu_sex"] = input("新的性别:")
stu["stu_score"] = int(input("新的成绩:"))
return True
print("您输入的学生学号不存在,修改失败!!!")
return False
def main():
while True:
print_menu()
ch = int(input("请输入您的选择:"))
match ch:
case 1:
print(stu_info)
case 2:
add_stu()
case 3:
del_stu()
case 4:
search_stu()
case 5:
modify_stu()
if ch == 6:
print("欢迎下次使用本学生信息管理系统!!!")
break
return None
main()
- 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
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
-----------------------------------------------------
-------------欢迎使用学生信息管理系统---------------------
-----------------1.遍历学生信息------------------------
-----------------2.添加学生信息------------------------
-----------------3.删除学生信息------------------------
-----------------4.查询学生信息------------------------
-----------------5.修改学生信息------------------------
-----------------6.退出系统---------------------------
-----------------------------------------------------
请输入您的选择:2
学号:1001
姓名:小王
性别:女
成绩:88
添加成功!!!
-----------------------------------------------------
-------------欢迎使用学生信息管理系统---------------------
-----------------1.遍历学生信息------------------------
-----------------2.添加学生信息------------------------
-----------------3.删除学生信息------------------------
-----------------4.查询学生信息------------------------
-----------------5.修改学生信息------------------------
-----------------6.退出系统---------------------------
-----------------------------------------------------
请输入您的选择:2
学号:1005
姓名:小李
性别:男
成绩:100
添加成功!!!
-----------------------------------------------------
-------------欢迎使用学生信息管理系统---------------------
-----------------1.遍历学生信息------------------------
-----------------2.添加学生信息------------------------
-----------------3.删除学生信息------------------------
-----------------4.查询学生信息------------------------
-----------------5.修改学生信息------------------------
-----------------6.退出系统---------------------------
-----------------------------------------------------
请输入您的选择:2
学号:1003
姓名:小吴
性别:女
成绩:99
添加成功!!!
-----------------------------------------------------
-------------欢迎使用学生信息管理系统---------------------
-----------------1.遍历学生信息------------------------
-----------------2.添加学生信息------------------------
-----------------3.删除学生信息------------------------
-----------------4.查询学生信息------------------------
-----------------5.修改学生信息------------------------
-----------------6.退出系统---------------------------
-----------------------------------------------------
请输入您的选择:1
[{'stu_id': 1001, 'stu_name': '小王', 'stu_sex': '女', 'stu_score': 88}, {'stu_id': 1005, 'stu_name': '小李', 'stu_sex': '男', 'stu_score': 100}, {'stu_id': 1003, 'stu_name': '小吴', 'stu_sex': '女', 'stu_score': 99}]
-----------------------------------------------------
-------------欢迎使用学生信息管理系统---------------------
-----------------1.遍历学生信息------------------------
-----------------2.添加学生信息------------------------
-----------------3.删除学生信息------------------------
-----------------4.查询学生信息------------------------
-----------------5.修改学生信息------------------------
-----------------6.退出系统---------------------------
-----------------------------------------------------
请输入您的选择:5
请输入要修改的学生学号:1005
新的学号:1002
新的姓名:小李
新的性别:男
新的成绩:98
-----------------------------------------------------
-------------欢迎使用学生信息管理系统---------------------
-----------------1.遍历学生信息------------------------
-----------------2.添加学生信息------------------------
-----------------3.删除学生信息------------------------
-----------------4.查询学生信息------------------------
-----------------5.修改学生信息------------------------
-----------------6.退出系统---------------------------
-----------------------------------------------------
请输入您的选择:1
[{'stu_id': 1001, 'stu_name': '小王', 'stu_sex': '女', 'stu_score': 88}, {'stu_id': 1002, 'stu_name': '小李', 'stu_sex': '男', 'stu_score': 98}, {'stu_id': 1003, 'stu_name': '小吴', 'stu_sex': '女', 'stu_score': 99}]
-----------------------------------------------------
-------------欢迎使用学生信息管理系统---------------------
-----------------1.遍历学生信息------------------------
-----------------2.添加学生信息------------------------
-----------------3.删除学生信息------------------------
-----------------4.查询学生信息------------------------
-----------------5.修改学生信息------------------------
-----------------6.退出系统---------------------------
-----------------------------------------------------
请输入您的选择:3
请输入所删除的学生学号:1001
-----------------------------------------------------
-------------欢迎使用学生信息管理系统---------------------
-----------------1.遍历学生信息------------------------
-----------------2.添加学生信息------------------------
-----------------3.删除学生信息------------------------
-----------------4.查询学生信息------------------------
-----------------5.修改学生信息------------------------
-----------------6.退出系统---------------------------
-----------------------------------------------------
请输入您的选择:1
[{'stu_id': 1002, 'stu_name': '小李', 'stu_sex': '男', 'stu_score': 98}, {'stu_id': 1003, 'stu_name': '小吴', 'stu_sex': '女', 'stu_score': 99}]
-----------------------------------------------------
-------------欢迎使用学生信息管理系统---------------------
-----------------1.遍历学生信息------------------------
-----------------2.添加学生信息------------------------
-----------------3.删除学生信息------------------------
-----------------4.查询学生信息------------------------
-----------------5.修改学生信息------------------------
-----------------6.退出系统---------------------------
-----------------------------------------------------
请输入您的选择:4
请输入要查询的学生学号:1003
stu_id: 1003
stu_name: 小吴
stu_sex: 女
stu_score: 99
-----------------------------------------------------
-------------欢迎使用学生信息管理系统---------------------
-----------------1.遍历学生信息------------------------
-----------------2.添加学生信息------------------------
-----------------3.删除学生信息------------------------
-----------------4.查询学生信息------------------------
-----------------5.修改学生信息------------------------
-----------------6.退出系统---------------------------
-----------------------------------------------------
请输入您的选择:6
欢迎下次使用本学生信息管理系统!!!
- 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
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131