python学生信息管理系统
参考资料统一为:@number
注册模块:
def logon():
print("欢迎来到注册页面");
username=input("请输入用户名称");
if len(username)<6:
print("用户名称不能小于6个字符");
a = input("是否继续注册:Y y or N n:")
if a == 'Y' or a == 'y':
logon();
elif a != 'y' or a != 'Y':
return
#exit(0)
print("欢迎下次继续")
else:
email=input("请输入email");
password=input("请输入密码");
if len(password)<8:
print("密码不能小于8个字符");
a = input("是否继续注册:Y y or N n:")
if a == 'Y' or a == 'y':
logon();
elif a != 'y' or a != 'Y':
return #返回空,实际意译为结束该函数运行:@1
else:
rpassword=input("请确认密码");
if password==rpassword:
print("注册成功");
data([username,' '+ email,' '+password +'\n']) #data为保存数据函数
else:
print("两次输入密码不一样");
logon();
保存数据函数:
#保存数据到文件
def data(data):
with open('1.txt', 'a') as x:
#判断是否可写
if x.writable(): #判断文件是否可写
x.writelines(data) #数据写入文件
登录用户和判断用户是否存在:
def login():
print("欢迎来到图书馆登录界面");
username = input("输入用户名:")
password = input("输入密码:")
with open('1.txt',"rb") as x:
result=x.readlines();
#print(result);
user_list=[i.decode() for i in result];
print(user_list)
for i in user_list:
info = i.split(' ');
#print(info);
# 输出每个info的第一个元素
for j in info:
a=j.split(" ");
#print(a);
if username==a[0] and password==a[2].rstrip('\r\n'):
print("登录成功");
else:
a = input("是否继续登录:Y y or N n:")
if a == 'Y' or a == 'y':
login()
elif a != 'y' or a != 'Y':
print("欢迎下次继续");
return
添加图书:
这是只添加一个书名
def add_book():
with open('book.txt',"a") as b:
line1=[]
q = 0;
if b.writable():
msg=input("请输入书名:")
#添加图书之前判断是否已经添加:
with open('book.txt','r',encoding='utf-8') as b1:
line=b1.readlines();
#print(line)
for i in line:
line1.append(i.rstrip());
#print(line1)
#print(len(line1))
for i in range (0,len(line1),1):
if msg==line1[i]:
q=q+1;
else:
q=q+0;
if q>0:
print("图书已经存在")
else:
print("可以添加图书")
add_data(msg)
print("添加成功")
print(q)
def add_data(msg):
with open('book.txt','a',encoding='utf-8') as b:
b.writable();
b.write('\n'+msg)
这是添加书的全部:
with open('book.txt',"a") as b:
line1=[]
line2=[]
q = 0;
if b.writable():
msg=input("请输入书名:")
#添加图书之前判断是否已经添加:
with open('book.txt','r',encoding='utf-8') as b1:
line=b1.readlines();
# print(line)
for i in line:
line1.append(i.rstrip());
print(line1)
# print(len(line1))
for i in range (0,len(line1),1):
line2=line1[i].split(' ');
# print(line2)
if msg == line2[0]:
q = q + 1;
else:
q = q + 0;
if q > 0:
print("图书已经存在")
else:
print("可以添加图书")
add_data([msg, ' ' + '0', ' ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + '\n']);
print("添加成功")
print(q)
思路:添加书本,首先要判断文件夹中是否有该书,
首先读出文件夹中存在的书,读出的列表带有‘\n’,需要通过便利输出去除‘\n’并=赋值给新的列表line1,
将新的列表遍历输出,并且以‘空格’为切片形成line2,此时line2[0]都是书名,然后进
行msg==line2[0]比较,满足条件的进行添加
图书查找
def find_book():
lb1=[];
lb2=[];
q=0
bname=input("请输入要查找的书本名称:")
with open('book.txt','r',encoding='utf-8') as b:
lb=b.readlines();
for i in range(0,len(lb),1):
lb1.append(lb[i].rstrip('\n'))
lb2=lb1[i].split(' ')
if bname==lb2[0]:
q=q+1;
else:
q=q+0;
# print(lb2)
print(q)
if q>=1:
print("图书存在")
运行结果