函数应用:学生管理系统
import time
import os
info_list = []
def print_menu():
print("---------------------------")
print(" 学生管理系统 V1.0")
print(" 1:添加学生")
print(" 2:删除学生")
print(" 3:修改学生")
print(" 4:查询学生")
print(" 5:显示所有学生")
print(" 6:退出系统")
print("---------------------------")
def add_new_info():
"""添加学生信息"""
global info_list
new_name = input("请输入姓名:")
new_tel = input("请输入手机号:")
new_qq = input("请输入QQ:")
for temp_info in info_list:
if temp_info['name'] == new_name:
print("此用户名已经被占用,请重新输入")
return
info = {}
info["name"] = new_name
info["tel"] = new_tel
info["qq"] = new_qq
info_list.append(info)
def del_info():
"""删除学生信息"""
global info_list
del_num = int(input("请输入要删除的序号:"))
if 0 <= del_num < len(info_list):
del_flag = input("你确定要删除么?yes or no")
if del_flag == "yes":
del info_list[del_num]
else:
print("输入序号有误,请重新输入")
def modify_info():
"""修改学生信息"""
global info_list
modify_num = int(input("请输入要修改的序号:"))
if 0 <= modify_num < len(info_list):
print("你要修改的信息是:")
print("name:%s, tel:%s, QQ:%s" % (info_list[modify_num]['name'],
info_list[modify_num]['tel'],info_list[modify_num]['qq']))
info_list[modify_num]['name'] = input("请输入新的姓名:")
info_list[modify_num]['tel'] = input("请输入新的手机号:")
info_list[modify_num]['qq'] = input("请输入新QQ:")
else:
print("输入序号有误,请重新输入")
def search_info():
"""查询学生信息"""
search_name = input("请输入要查询的学生姓名:")
for temp_info in info_list:
if temp_info['name'] == search_name:
print("查询到的信息如下:")
print("name:%s, tel:%s, QQ:%s" % (temp_info['name'],
temp_info['tel'], temp_info['qq']))
break
else:
print("没有您要找的信息....")
def print_all_info():
"""遍历学生信息"""
print("序号\t姓名\t\t手机号\t\tQQ")
i = 0
for temp in info_list:
print("%d\t%s\t\t%s\t\t%s" % (i, temp['name'], temp['tel'], temp['qq']))
i += 1
def main():
"""用来控制整个流程"""
while True:
print_menu()
num = input("请输入要进行的操作(数字)")
if num == "1":
add_new_info()
elif num == "2":
del_info()
elif num == "3":
modify_info()
elif num == "4":
search_info()
elif num == "5":
print_all_info()
elif num == "6":
exit_flag = input("亲,你确定要退出么?~~~~(>_<)~~~~(yes or no) ")
if exit_flag == "yes":
break
else:
print("输入有误,请重新输入......")
input("\n\n\n按回车键继续....")
os.system("clear")
main()