題目
功能描述:
圖書管理系統項目功能描述:
(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 = Falsewhile True:info = users.readline()if info:if (name in info):isExist = Truebreakelse:breakusers.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 = 0while True:info = readUsers.readline()if info:if ((name in info) and (password in info)):condition = 1breakelif ((name in info) or (password in info)):condition = 2breakelse:condition = 3else:breakif condition == 1:print(name + '登錄成功!')return Trueelif condition == 2:print('用戶名或密碼錯誤!')return Falseelif condition == 3:print('您還未注冊!')return False# 創建圖書模塊
def create_books(name):id = count(booksPath)book = open(booksPath, 'r', encoding='utf-8')isExist = Falsewhile True:info = book.readline()if info:if (name in info):isExist = Truebreakelse:breakbook.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 = Falsewhile True:book = readBooks.readline()userbook = readUsersBook.readline()if book or userbook:if ((bookname in book) and (bookname in userbook)):condition = Truebreakelse:breakreadUsersBook.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('已成功退出!')breakelse:print('輸入非法,請重新輸入!')main()