python实验练习-图书管理系统(使用文件来实现)
Posted DQ_CODING
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python实验练习-图书管理系统(使用文件来实现)相关的知识,希望对你有一定的参考价值。
题目
功能描述:
图书管理系统项目功能描述:
(1)创建txt文本文档,包含如下:
① books.txt:保存有一些书籍名称;
② users.txt:用于保存用户相关的信息;
③ users_book.txt:记录哪些用户借了哪些书
(2)程序包含用户注册、用户登录、查看图书、借阅图书等相关图书管理系统的功能。
(3)可根据自己对图书管理系统的理解增加其它功能。
将问题分析划分为如下内容完成:
(1)分别编写用户注册(register)模块、用户登录(login)模块、查看图书(show_books)模块、借阅图书(borrow_book)模块,并将成功注册的用户名和密码保存在文件users.txt中,已有书籍名称保存在books.txt中,哪些用户借阅了哪些图书保存在users_book.txt中;
(2)主函数中可实现根据提示依次调用用户注册、用户登录、查看图书、借阅图书等功能。
代码
# author:dq
# project:PythonProject
# date:2021年11月04日
# function:
# ① books.txt:保存有一些书籍名称;
# ② users.txt:用于保存用户相关的信息;
# ③ users_book.txt:记录哪些用户借了哪些书
import os
# 创建txt文件
def create(path):
file = open(path, 'a')
file.close()
booksPath = "./book.txt"
usersPath = "./users.txt"
usersBookPath = "./users_book.txt"
# create(booksPath)
# create(usersPath)
# create(usersBookPath)
# 计算已有多少数量
def count(path):
read = open(path, 'r', encoding='utf-8')
count = len(read.readlines())
read.close()
return count
# 用户注册(register)模块、
def register(name, password):
id = count(usersPath)
users = open(usersPath, 'r', encoding='utf-8')
isExist = False
while True:
info = users.readline()
if info:
if (name in info):
isExist = True
break
else:
break
users.close()
if isExist == True:
print('此账号已注册,可直接进行登录!')
elif isExist == False:
wUser = open(usersPath, 'a', encoding='utf-8')
i = str(id + 1) + '\\t' + name + '\\t' + password + '\\n'
wUser.writelines(i)
print(name + '已成功注册!')
wUser.close()
# 用户登录(login)模块
def login(name, password):
readUsers = open(usersPath, 'r', encoding='utf-8')
condition = 0
while True:
info = readUsers.readline()
if info:
if ((name in info) and (password in info)):
condition = 1
break
elif ((name in info) or (password in info)):
condition = 2
break
else:
condition = 3
else:
break
if condition == 1:
print(name + '登录成功!')
return True
elif condition == 2:
print('用户名或密码错误!')
return False
elif condition == 3:
print('您还未注册!')
return False
# 创建图书模块
def create_books(name):
id = count(booksPath)
book = open(booksPath, 'r', encoding='utf-8')
isExist = False
while True:
info = book.readline()
if info:
if (name in info):
isExist = True
break
else:
break
book.close()
if isExist == True:
print('此书籍在图书馆里已存在!')
elif isExist == False:
wBooK = open(booksPath, 'a', encoding='utf-8')
i = str(id + 1) + '\\t' + name + '\\n'
wBooK.writelines(i)
print(name + '成功放入图书馆!')
wBooK.close()
# 查看图书(show_books)模块
def show_books():
read = open(booksPath, 'r', encoding='utf-8')
print('图书馆现有的图书如下:')
while True:
info = read.readline()
if info:
print(info)
else:
break
# 借阅图书(borrow_book)模块,
def borrow_book(username, bookname):
readUsersBook = open(usersBookPath, 'r', encoding='utf-8')
readBooks = open(booksPath, 'r', encoding='utf-8')
id = count(usersBookPath)
condition = False
while True:
book = readBooks.readline()
userbook = readUsersBook.readline()
if book or userbook:
if ((bookname in book) and (bookname in userbook)):
condition = True
break
else:
break
readUsersBook.close()
readBooks.close()
if condition == True:
print(bookname + '已经被借出了!')
elif condition == False:
wUserBook = open(usersBookPath, 'a', encoding='utf-8')
i = str(id + 1) + '\\t' + username + '\\t' + bookname + '\\n'
wUserBook.writelines(i)
wUserBook.close()
print(username + '已成功借到' + bookname)
# 程序包含用户注册、用户登录、查看图书、借阅图书等相关图书管理系统的功能。
def main():
print('欢迎使用图书馆!')
print('请选择要使用的功能(1-5):')
print('1.用户注册')
print('2.用户登录')
print('3.查看图书')
print('4.捐赠图书')
print('5.借阅图书')
print('6.退出')
while True:
num = input('功能选择:')
if (num == '1'):
name = input('请输入要注册的用户名')
password = input('请输入要注册的密码')
register(name, password)
elif (num == '2'):
name = input('请输入用户名')
password = input('请输入密码')
condition = login(name, password)
elif (num == '3'):
show_books()
elif (num == '4'):
name = input('请输入要捐赠的图书')
create_books(name)
elif (num == '5'):
name = input('请输入用户名')
bookname = input('请输入要借阅的图书')
borrow_book(name, bookname)
elif (num == '6'):
print('已成功退出!')
break
else:
print('输入非法,请重新输入!')
main()
以上是关于python实验练习-图书管理系统(使用文件来实现)的主要内容,如果未能解决你的问题,请参考以下文章