废话不多说,直接上代码。
import sys
# 打印书库中的所有书的函数
def book_detail(books):
for book_name, book_info in books.items():
if book_info is not None:
print('bookName:{} ISBN:{} title:{} author:{} number:{} borrowNumber:{}'
.format(book_name, book_info['ISBN'], book_info['title'], book_info['author'], book_info['number'],
book_info['borrow_num']))
# 检查借书情况
def check_borrow(book):
for book_info in books.values():
# {}跟%s差不多的效果,你翻译的时候注意
print('{}被借出{}本'.format(book_info['title'], book_info['borrow_num']))
# 查找书籍ISBN
def search_book(books):
flag = 'y'
# 添加书籍
while (flag == 'y'):
title = input('输入你想查找的书籍的名字:')
i = 0
for book_info in books.values():
i = 1
if title in book_info.values():
print('您所查找的书籍的信息如下:')
print('title:{} ISBN:{}'.format(book_info['title'], book_info['ISBN']))
if i == 0: # i=0说明没有找到符合输入的书籍
print('没有该书!')
# 防止出现错误输入
while (True):
flag = input('''如果你想要继续查找书籍请输入'y',想退出请输入'n':''')
if flag == 'y' or flag == 'n':
break
else:
print('请输入小写的y或n!')
# 添加书籍数量的函数
def add_book(books):
flag = True
# 添加书籍
while (flag):
next_flag = True
isbn = input('输入你想添加的书籍的ISBN编号或者输入 exit 退出:')
if isbn == 'exit':
break
no_book = True
for book_info in books.values():
if isbn in book_info.values():
no_book = False
print("1. 修改在库数目")
print('2. 修改书籍名称')
print('3. 修改书籍作者')
print('4. 退出')
option_menu = input('书籍在库, 请输入要进行的操作:')
if option_menu == '4':
break
elif option_menu == '1':
try:
add_num = eval(input('请输入添加数量:'))
except Exception:
print("添加书籍在库数目时请输入数字")
next_flag = False
break
if add_num < 0:
print('输入错误, 请输入正整数')
next_flag = False
break
book_info['number'] += int(add_num)
elif option_menu == '2':
new_title = input('请输入需要修改成的名称:')
book_info['title'] = new_title
elif option_menu == '3':
new_author = input('请输入需要修改成作者的名称:')
book_info['author'] = new_author
else:
print('请输入正确的操作代码')
next_flag = False
break
else:
no_book = True
if no_book:
new_book_info = {}
title = input('输入你想添加的书籍的名称:')
author = input('输入你想添加的书籍的作者:')
# 需要做异常处理,防止错误输入
try:
number = eval(input('输入你想添加的书籍的在库数目:'))
except Exception as e:
print("添加书籍在库数目时请输入数字")
next_flag = False
new_book_info['ISBN'] = isbn
new_book_info['title'] = title
new_book_info['author'] = author
new_book_info['number'] = number
new_book_info['borrow_num'] = 0
key = 'book_' + str(len(books) + 1)
books[key] = new_book_info
print('书籍【' + title + '】添加成功')
# 防止出现错误输入
while (next_flag):
flag_continue = input('''如果你想要继续添加书籍请输入'y',想退出请输入'n':''')
if flag_continue.lower() == 'y':
break
else:
flag = False
break
# 借书函数
def borrow_book(books):
flag = 'y'
# 添加书籍
while (flag == 'y'):
isbn = input('输入你想借的书籍的ISBN编号:')
i = 0
for book_info in books.values():
i = 1
if isbn in book_info.values():
b_num = input('请输入要借的的数量:')
if book_info['number'] >= int(b_num):
book_info['number'] -= int(b_num)
book_info['borrow_num'] += int(b_num)
else:
print('对不起,库存不足!')
if i == 0: # i=0说明没有找到符合输入的ISBN号
print('没有该ISBN编号!')
# 防止出现错误输入
while (True):
flag = input('''如果你想要继续借书籍请输入'y',想退出请输入'n':''')
if flag == 'y' or flag == 'n':
break
else:
print('请输入小写的y或n!')
# 还书函数
def return_book(books):
flag = 'y'
# 添加书籍
while (flag == 'y'):
isbn = input('Enter the ISBN number of the book you want to return')
i = 0
for book_info in books.values():
i = 1
if isbn in book_info.values():
r_num = input('Please enter the amount to be returned')
if book_info['borrow_num'] >= int(r_num):
book_info['number'] += int(r_num)
book_info['borrow_num'] -= int(r_num)
else:
print('''I'm sorry, there aren't so many books on loan''')
if i == 0:
print('sorry we do not have this ISBN')
while (True):
flag = input('''If you want to continue returning books, type 'y' and 'n' to exit:''')
if flag == 'y' or flag == 'n':
break
else:
print('Please enter a lowercase y or n!')
if __name__ == '__main__':
# 初始图书馆图书信息
books = {
'book_1': {
'ISBN': '12345678910121',
'title': '假如给我三天光明',
'author': '海伦凯勒',
'number': 2,
'borrow_num': 0},
'book_2': {
'ISBN': '12345678910122',
'title': '红与黑',
'author': '司汤达',
'number': 2,
'borrow_num': 0},
'book_3': {
'ISBN': '12345678910123',
'title': '鲁滨逊漂流记',
'author': '笛福',
'number': 2,
'borrow_num': 0},
}
# 初始化用户信息
print('1. >>>>>>管理员<<<<<<')
print('2. >>>>>>普通用户<<<<<<')
user = input('^^^^^^请输入你的身份^^^^^^')
if user == '1':
password = input('^^^^^^请输入你的登陆密码^^^^^^')
if password == 'admin':
print("========欢迎登陆========")
else:
print('密码错误,自动退出系统')
sys.exit(0)
pass
print('0. ******查 看 书 籍******')
print('1. ******添 加 书 籍******')
print('4. ******查看书籍借阅情况******')
print('5. ******查 书******')
print('6. ******退 出 系 统******')
elif user == '2':
print("========欢迎光临结束系统========")
print("========关于我们的书籍========")
print('0. ******查 看 书 籍******')
print('2. ******借 书******')
print('3. ******还 书******')
print('4. ******查看书籍借阅情况******')
print('5. ******查 书******')
print('6. ******退 出 系 统******')
else:
print('输入错误,自动退出系统')
sys.exit(0)
# 操作选项
option = input("\n请选择操作选项: ")
while (option != '6'):
if option == '0':
print('~~~~~~图 书 列 表~~~~~~')
book_detail(books)
elif option == '1':
print('您选择的是:【添加书籍】')
add_book(books)
elif option == '2':
print('您选择的是:【借书】')
borrow_book(books)
elif option == '3':
print('您选择的是:【还书】')
return_book(books)
elif option == '4':
print('您选择的是:【查看书籍借阅情况】')
check_borrow(books)
elif option == '5':
print('您选择的是:【查书】')
search_book(books)
else:
print('不好意思,我不知道您要干什么,请重新选择')
if user == '1':
print('0. ******查 看 书 籍******')
print('1. ******添 加 书 籍******')
print('4. ******查看书籍借阅情况******')
print('5. ******查 书******')
print('6. ******退 出 系 统******')
elif user == '2':
print('0. ******查 看 书 籍******')
print('2. ******借 书******')
print('3. ******还 书******')
print('4. ******查看书籍借阅情况******')
print('5. ******查 书******')
print('6. ******退 出 系 统******')
option = input("\n请选择操作选项: ")