本文主要提供Python学生信息管理的代码,包含增删改查的功能。重要部分已经加注释,代码如下:
def start_print():
print('-----请输入序号选择功能-----')
print('1、添加学生')
print('2、删除学生')
print('3、修改学生')
print('4、查询学生')
print('5、显示所有学生')
print('6、退出系统')
print('-' * 26)
print('--------初始化中--------')
id = True
try:
f = open('学生成绩数据.txt','w')
data_title = ['学号', '姓名','身高','体重','高数成绩','英语成绩','计算机成绩','\n']
data_content = [['001', '学生1','170','63','80','71','95','\n'],
['002', '学生2','173','60','88','89','91','\n'],
['003','学生3','175','65','83','81','90','\n'],
['004','学生4','179','67','59','75','78','\n'],
['005','学生5','180','74','66','68','81','\n'],]
for i in data_title:
if i == '\n':
f.write(i)
else:
f.write('{:<5}'.format(i))
for i in data_content:
for j in i:
if j == '\n':
f.write(j)
elif i.index(j) < 4 :
f.write('{:<6}'.format(j))
elif i.index(j) == 4:
f.write('{:>6}'.format(j))
elif i.index(j) > 4:
f.write('{:>8}'.format(j))
f.close()
print('--------初始化成功------')
except:
print('--------初始化失败------')
id = False
class Student:
global data_content
global data_title
def add(self):
new_id = input("输入学号:")
new_name = input("输入姓名:")
new_heigh = input("输入身高:")
new_weigh = input("输入体重:")
new_math = input("输入高数成绩:")
new_english = input("输入英语成绩:")
new_computer = input("输入计算机成绩:")
for i in data_content:
if new_name == i[1]:
print("此用户已经存在,请勿重复添加")
return
data_content.append([new_id,new_name,new_heigh,new_weigh,new_math,new_english,new_computer])
f = open('学生成绩数据.txt','w')
for i in data_title:
f.write(i)
for i in data_content:
for j in i:
f.write(j)
f.close()
print('-----写入成功-----')
def delete(self):
del_name = input("请输入要删除的学生的姓名:")
for i in data_content:
if del_name == i[1]:
data_content.remove(i)
break
else:
print('该学生不存在!')
return
print('-----删除成功-----')
def change(self):
change_name = input("请输入要修改的学生的姓名:")
for i in data_content:
if change_name == i[1]:
xg=input("请输入修改的项目:")
xg_index = data_title.index(xg)
i[xg_index] = input("请输入新的数据:")
break
else:
print("该学生不存在")
print(data_content)
def search(self):
search_name = input("请输入要查找的学生姓名:")
global data_content
for i in data_content:
if search_name == i[1]:
b=data_content.index(i)
print("找到该学生的信息如下:")
print(data_content[b])
break
else:
print("该学生不存在!")
def print_all(self):
print('学号','姓名',' 身高','体重','高数成绩','英语成绩','计算机成绩')
for i in data_content:
for j in i:
print(j,end=' ')
def func(self,num):
if num == 1:
self.add()
if num == 2:
self.delete()
if num == 3:
self.change()
if num == 4:
self.search()
if num == 5:
self.print_all()
while id:
start_print()
b=Student()
a=eval(input("请输入序号:"))
if a == 6:
exit_flag = input("确定要退出吗?yes/no?:")
if exit_flag == 'yes':
break
b.func(a)
希望对大家的大作业有所帮助,谢谢观看