0
我想實現一個小型的圖書館管理系統使用python。我們有一個功能列表。我堅持的功能是這部分:簡單的圖書館管理系統使用python
- 實現一個Python函數,該函數將一本書添加到庫中。 您的功能應該要求書籍ISBN,書名,作者以及購買了多少份。該功能應更新庫存庫(詞典)以包含新書。 如果該書已在圖書館中,系統應更新數量。
我的字典如下。重點= ISBN,值=拷貝/題名/作者
library = {4139770544441: [5,'Hello World','John'],
4139770544442: [2,'Red Sky','Mary'],
4139770544443: [8,'The Road','Chris']}
下面是功能我要補充一本書:
def add_book(key, amount, library):
for current_key in library.keys():
if current_key == key:
library[current_key] = library[current_key] + amount
# amount updated
# get out of the loop and the function
return
#item doesn't exist in the list, add it with the specified amount
library[key] = amount
#User inputs new book titles
enter_copies = int(input('Please enter number of copies to add: '))
enter_title = input('Please enter the Title of the book: ')
enter_author = input('Please enter the Author of the book: ')
#relates to add_book Function
add_book(enter_book, [enter_copies, enter_title, enter_author], library)
如果它是一個新的書,我希望它在添加到字典,如果它是一本現有的書,我希望它增加份數。然而,正在發生的事情是,只是在末尾添加了isbn(key)和值,而不管它是否存在。任何幫助將不勝感激。
記得要保存返回值'library'到DB(任何種類的數據庫使用的是帶),否則你下次獲得'庫dict'時間,它和以前一樣,沒有任何改變。 –
感謝怪異的蜂蜜,完美的工作。 – vinnievienna