import sys# 打印书库中的所有书的函数def book_detail: for book_name, book_info in books.items: if book_info is not None: print)# 检查借书情况def check_borrow: for book_info in books.values: # {}跟%s差不多的效果,你翻译的时候注意 print)# 查找书籍ISBNdef search_book: flag = "y" # 添加书籍 while : title = input i = 0 for book_info in books.values: i = 1 if title in book_info.values: print print) if i == 0: # i=0说明没有找到符合输入的书籍 print # 防止出现错误输入 while : flag = input if flag == "y" or flag == "n": break else: print# 添加书籍数量的函数def add_book: flag = True # 添加书籍 while : next_flag = True isbn = input if isbn == "exit": break no_book = True for book_info in books.values: if isbn in book_info.values: no_book = False print print print print option_menu = input if option_menu == "4": break elif option_menu == "1": try: add_num = eval) except Exception: print next_flag = False break if add_num < 0: print next_flag = False break book_info["number"] += int 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) 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 + 1) books[key] = new_book_info print # 防止出现错误输入 while : flag_continue = input if flag_continue.lower == "y": break else: flag = False break# 借书函数def borrow_book: flag = "y" # 添加书籍 while : isbn = input i = 0 for book_info in books.values: i = 1 if isbn in book_info.values: b_num = input if book_info["number"] >= int: book_info["number"] -= int book_info["borrow_num"] += int else: print if i == 0: # i=0说明没有找到符合输入的ISBN号 print # 防止出现错误输入 while : flag = input if flag == "y" or flag == "n": break else: print# 还书函数def return_book: flag = "y" # 添加书籍 while : isbn = input i = 0 for book_info in books.values: i = 1 if isbn in book_info.values: r_num = input if book_info["borrow_num"] >= int: book_info["number"] += int book_info["borrow_num"] -= int else: print if i == 0: # i=0说明没有找到符合输入的ISBN号 print # 防止出现错误输入 while : flag = input if flag == "y" or flag == "n": break else: printif __name__ == "__main__": # 初始图书馆图书信息 books = { "book_1": { "ISBN": "12345678910121", "title": "pythone", "author": "james", "number": 2, "borrow_num": 0}, "book_2": { "ISBN": "12345678910122", "title": "computing", "author": "kevin", "number": 2, "borrow_num": 0}, "book_3": { "ISBN": "12345678910123", "title": "java", "author": "steven", "number": 2, "borrow_num": 0}, } # 初始化用户信息 print print user = input if user == "1": password = input if password == "admin": print else: print sys.exit pass print print print print print elif user == "2": print print print print print print print print else: print sys.exit # 操作选项 option = input while : if option == "0": print book_detail elif option == "1": print add_book elif option == "2": print borrow_book elif option == "3": print return_book elif option == "4": print check_borrow elif option == "5": print search_book else: print # option_continue = input") # # if option_continue.lower == "y" or option_continue.lower == "yes": # continue # elif option_continue.lower == "n" or option_continue.lower == "no": # print # break if user == "1": print print print print print elif user == "2": print print print print print print option = input