【发布时间】:2021-04-29 15:26:46
【问题描述】:
我有一个图书库程序,它的类读取包含@987654321@ 和name 的文件并将它们分配给self 对象作为id 和card holder。然后有借书的自我对象,所以我可以识别哪个用户借了哪本书。
问题出在main 函数中,当用户还书时,我不知道如何更新自我借书对象以从对象中删除该书,因此自我id 将没有书借给它。
这是类的外观:
#loan time length is 3 weeks by default
LOAN_TIME = 3
class Librarycard:
def __init__(self, card_id, card_holder):
self.__id = card_id
self.__holder = card_holder
#a dictionary that will contain the full book : loan time, updated in later function
self.__loan = {}
def return_book(self, book):
del self.__loan[book]
print('returned')
return
这是我的main功能部分,涉及图书借阅:
def main():
command = input("Command: ")
#borrowed books main list to check if book borrowed or not
borrowed_books = []
if command == "R":
book = input("Book code: ")
if book not in borrowed_books:
print('This book has not been borrowed by anyone')
else:
del borrowed_books[book]
print('book returned')
# this is where I try to enter the function from the class to update the object
# dictionary
book.return_book(book)
if __name__ == "__main__":
main()
【问题讨论】:
标签: python function class dictionary object