import os
#用户注册函数
def sign_in():
print("欢迎来到用户注册界面")
username = input("请输入你的用户名:")
password = input('请输入你的密码:')
repassword = input("请再次输入你的密码:")
if password==repassword:
with open(r"D:\p1\user.txt", "rb") as rstream:
while True:
filelist = rstream.readline()
if not filelist:
break
users = '{} {}\n'.format(username, password)
if users == filelist:
print("对不起该用户已经注册过了")
break
with open(r"D:\p1\user.txt", 'a') as wstream:
wstream.write('{} {}\n'.format(username, password))
print("注册成功")
else:
print("两次密码不一致,请重新输入")
# 用户登入界面
def login():
print("欢迎来到用户登入界面!")
username = input("请输入你的用户名:")
password = input("请输入你的密码:")
with open(r"D:\p1\user.txt") as stream:
while True:
filelist = stream.readline()
if not filelist:
print("用户登入有错误!")
break
users = '{} {}\n'.format(username, password)
if users == filelist:
print("用户登入成功!")
return 1
break
#显示所有图书
def show_books():
print("******图书馆含有的图书**************")
with open( r'D:\p1\books.txt',"r",encoding='utf-8') as stream:
books = stream.readlines()
for book in books:
print(book,end="")
#借书
def borrow_book(book1):
transit = "" # 创立空的字符串用于储存文件
with open(r'D:\p1\books.txt', "r", encoding='utf-8') as stream:
books = stream.readlines()
for book in books:
if book1 not in book:
print("对不起,没有该书")
elif book1 in book:
continue
else:
transit += book # 将数据写入过渡的字符串中
with open(r'D:\p1\books.txt', "w", encoding='utf-8') as wstream:
wstream.write(transit)
#还书
def return_book(book1):
with open(r'D:\p1\books.txt', "a", encoding='utf-8') as stream:
stream.write('\n{}\n'.format(book1))
#查书
def look_book(book1):
with open(r'D:\p1\books.txt', "r", encoding='utf-8') as stream:
books = stream.readlines()
for book in books:
if book1 in book:
print("该书在图书馆内")
break
else:
print("对不起查无此书")
print("*******欢迎来到图书馆登入系统******")
while True:
print('功能:1,登入 2.注册 ')
str = input('请输入你要做的事情:')
if str == "1":
a=login()
while True:
print("***********************")
print('''功能:
1,借书 2,还书 3,查书 4,显示所有图书 5,退出登入
''')
key = input("请输入你要做的事情:")
if key == '1':
book1 = input("请输入你要借的书的书名:")
borrow_book(book1)
print("借书成功!")
if key == '2':
book1 = input("请输入你要还的书的书名")
return_book(book1)
print("还书成功!")
if key == '3':
book1 = input("请输入你要查的书的书名")
look_book(book1)
if key == '4':
show_books()
else:
print("成功退出界面")
print(key)
if str == "2":
sign_in()
else:
print("对不起,小图还没有学会该功能呢!")
print(str)
图书管理系统 python
2021/12/23 21:20:54