python实现的学生信息管理系统,大一上学期时写的小程序,分享一波(无GUI界面设计)
首先创建一个data.txt在py文件的同级目录,用于存储录入的学生信息数据
并导入所需库
import os
filename = 'data.txt'
创建main函数用于选择调用其他方法
这里使用while循环来使菜单在程序运行时循环出现
def main():
while(True):
menu()
option = input("Please choose:")
if option == '1':
show()
elif option == '2':
insert()
elif option == '3':
delete()
elif option == '4':
update()
elif option == '5':
search()
elif option == '6':
total()
else:
print('您已经退出学生信息管理系统!!')
break
简简单单一个函数用来输出展示菜单
# 开始菜单
def menu():
print("################### Student Information Management System\t################")
print("################### Menu\t \t\t\t\t #################\n"
"################### 1、View all student information\t\t ##################\n"
"################### 2、Input student information \t \t ##################\n"
"################### 3、Delete student information \t ##################\n"
"################### 4、Modify student information \t ##################\n"
"################### 5、Search Student Information \t ##################\n"
"################### 6、Count the total number of students\t ##################")
读取文件并输出
# 查看所有学生信息
def show():
student_new = []
if os.path.exists(filename):
# 打开文件
with open(filename,'r') as file:
studentList = file.readlines()
for list in studentList:
student_new.append(eval(list))
if student_new:
print("student_no\t\tname\t\tsex\t\tage")
for i in student_new:
print(i.get('no') + '\t' + i.get('name') + '\t' + i.get('gender') + '\t' + i.get('age') + '\t')
写入文件,这里新增了判断是否继续添加的条件。
def insert():
studentList = []
flag = True
while flag:
no = input("Please enter your student id: ")
name = input("Please enter name: ")
gender = input("Please enter sex: ")
age = input("Please enter age:")
student = {'no' :no,'name':name,'gender':gender,'age':age,}
studentList.append(student)
more = input("Whether to continue to add students?(y/n):")
if more == 'y':
flag = True
else:
flag = False
student_txt = open(filename,'a')
for student in studentList:
student_txt.write(str(student)+'\n')
student_txt.close()
删除文件中的值,按字典的键值来查找
def delete():
flag = True
while flag:
studentNo = input("请输入要删除的学生学号:")
if studentNo != '':
if os.path.exists(filename):
with open(filename,'r') as rfile:
student_menu = rfile.readlines()
else:
student_menu = []
if student_menu:
with open(filename,'w') as wfile:
d = {}
for s in student_menu:
d = dict(eval(s))
if d['no'] != studentNo:
wfile.write(str(d) + '\n')
isDel = False
else:
isDel = True
if isDel:
print("学号为" + studentNo + "的学生已经被删除!")
else:
print("没有找到学号为" + studentNo + "的学生")
continue
more = input("是否继续删除?(y/n):")
if more =='y':
flag = True
else:
flag = False
先展示数据,再读取并进行写入
# 修改学生信息
def update():
flag = True
show()
stuentNo = input("请输入要修改的学生学号:")
# 读取文件
if os.path.exists(filename):
with open(filename,'r') as rfile:
student_menu = rfile.readlines()
else:
return
with open(filename,'w') as wfile:
d = {}
for s in student_menu:
d = dict(eval(s))
if d['no'] == stuentNo:
while flag:
try:
d["no"] = input('请输入学号:')
d["name"] = input("请输入名字:")
d["gender"] = input("请输入性别:")
d["age"] = input("请输入年龄:")
wfile.write(str(d) + "\n")
print("修改成功!")
except:
print("输入无效!请重新输入!")
wfile.write(str(d) + "\n")
continue
more = input("是否继续修改?(y/n):")
if more == 'y':
flag = True
else:
flag = False
else:
wfile.write(str(d) + "\n")
这里可以按学号查或者按姓名查,因为我们可以查指定的键
# 查找学生信息
def search():
isFlag = True
while isFlag:
no = ''
name = ''
# 请用户输入
mode = input('按学号查询请按1;按姓名查询请按2:')
if mode == '1':
no = input('请输入学生学号:')
elif mode == '2':
name = input('请输入学生的姓名:')
else:
print('您的输入有误,请重新输入!')
continue
# 读取文件
if os.path.exists(filename):
with open(filename,'r') as file:
studentList = file.readlines()
for list in studentList:
d = dict(eval(list))
if name !='':
if d['name'] == name:
print('学号\t姓名\t性别\t年龄')
print (d.get('no') + '\t' + d.get('name') + '\t' + d.get('gender') + '\t' + d.get('age') + '\t')
if no !='':
if d['no'] == no:
print('学号\t姓名\t性别\t年龄')
print(d.get('no') + '\t' + d.get('name') + '\t' + d.get('gender') + '\t' + d.get('age') + '\t')
more = input("是否继续查询?(y/n):")
if more == 'y':
isFlag = True
else:
isFlag = False
统计列表长度
def total():
if os.path.exists(filename):
with open(filename,'r') as file:
studentList = file.readlines()
if studentList:
print("--共有%d名学生"% len(studentList))
else:
print("还没有录入学生信息")
main()