大家好,又见面了,我是你们的朋友全栈君。
学员管理系统是刚接触python时算是一个比较难的小项目,毕竟第一次接触这样的思维逻辑,不过用心学起来还是很有趣的,发现乐在其中,也就不觉得难了。下面给大家分享一下学员管理系统较为完整的代码
1.主程序
import sys ,datetime
stulist = []
stuNumlist = []
userdata = {}
while True:
userInput = showmenu()
if userInput == "1":
addStu()
elif userInput == "2":
delStu()
elif userInput == "3":
revStu()
elif userInput == "4":
queryStu()
elif userInput == "5":
sys.exit()
else:
print("输入了错误的信息!")
continue
2.打印菜单
def showmenu():
print("============================================学员管理系统======================================================")
print("# * * * * * * 1.添加新学员 * * * * * * * * * #")
print("# * * * * * * 2.删除学员 * * * * * * * * * #")
print("# * * * * * * 3.修改学员 * * * * * * * * #")
print("# * * * * * 4.查询学员 * * * * * * * * *#")
print("# * * * * * * 5.按Q退出系统 * * * * * * * * #")
print("=============================================================================================================")
userInput = input("请输入你要进行操作的操作编码:")
return userInput
3.添加学员
def addStu():
while True:
user_name = input("请输入学员姓名,输入q退出:").strip().lower()
if user_name in stulist:
print("此学生已存在!请重新输入。")
continue
if len(user_name) == 0:
print("学员姓名不能为空!")
if user_name == "q":
break
user_age = input("请输入学员年龄:").strip()
if int(user_age) < 0 or int(user_age) > 100:
print("错误的年龄!")
continue
user_num = input("请输入学员学号:").strip()
if user_num in stuNumlist:
print("重复的学号!")
continue
user_sex = input("请输入学员性别[f|m]:").strip().lower()
if user_sex != "f" and user_sex != "m":
print("错误的性别!")
continue
userdata[user_name] = {"name":user_name,"age":user_age,"num":user_num,"sex":user_sex}
userdata[user_num] = {"name":user_name,"age":user_age,"num":user_num,"sex":user_sex}
stulist.append(user_name)
stuNumlist.append(user_num)
print("学员添加完成!")
time.sleep(1.5)
break
4.删除学员
def delStu():
while True:
num = 1
for neer in stulist:
print(num,neer)
num += 1
userinput = input("请选择要删除的学员编号,返回请按y,退出请按b:").strip().lower()
if userinput == "y":
break
elif userinput == "b":
sys.exit()
elif stulist[int(userinput)-1] in stulist:
del userdata[stulist[int(userinput)-1]]
del userdata[stuNumlist[int(userinput)-1]]
del stulist[int(userinput)-1]
del stuNumlist[int(userinput)-1]
print("删除成功!")
time.sleep(1.5)
break
5.修改学员信息
def revStu():
while True:
num = 1
for neer in stulist:
print(num, neer)
num += 1
userinput = input("请选择要修改的学员编号,返回请按y,退出请按b:").strip().lower()
if userinput == "y":
break
elif userinput == "b":
sys.exit()
elif stulist[int(userinput) - 1] in stulist:
user_name = input("请输入学员姓名:").strip().lower()
if user_name in stulist:
print("此学生已存在!请重新输入。")
continue
if len(user_name) == 0:
print("学员姓名不能为空!")
user_age = input("请输入学员年龄:").strip()
if int(user_age) < 0 or int(user_age) > 100:
print("错误的年龄!")
continue
user_num = input("请输入学员学号:").strip()
if user_num in stuNumlist:
print("重复的学号!")
continue
user_sex = input("请输入学员性别[f|m]:").strip().lower()
if user_sex != "f" and user_sex != "m":
print("错误的性别!")
continue
del userdata[stulist[int(userinput) - 1]]
del userdata[stuNumlist[int(userinput) - 1]]
del stulist[int(userinput) - 1]
del stuNumlist[int(userinput) - 1]
userdata[user_name] = {"name": user_name, "age": user_age, "num": user_num, "sex": user_sex}
userdata[user_num] = {"name": user_name, "age": user_age, "num": user_num, "sex": user_sex}
stulist.append(user_name)
stuNumlist.append(user_num)
print("学员信息修改成功!")
time.sleep(1.5)
break
6.查询学员信息
def queryStu():
userinput = input("请输入查询的学员姓名或学号:").strip()
if userinput in stuNumlist:
name = userdata[userinput]["name"]
age = userdata[userinput]["age"]
num = userdata[userinput]["num"]
sex = userdata[userinput]["sex"]
print("学员姓名是:%s ,年龄是:%s ,性别是:%s ,性别为:%s" % (name, age, sex, num))
time.sleep(2)
elif userinput in stulist:
name = userdata[userinput]["name"]
age = userdata[userinput]["age"]
num = userdata[userinput]["num"]
sex = userdata[userinput]["sex"]
print("学员姓名是:%s ,年龄是:%s ,性别是:%s ,性别为:%s" % (name, age, sex, num))
time.sleep(2)
elif userinput not in stuNumlist:
print("没有该学员")
elif userinput not in stulist:
print("没有该学员")
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/192612.html原文链接:https://javaforall.cn