英文:
How to call function inside function in class
问题
# def deletebooksfromdisplay(self,bookname):
# self.books.remove(bookname)
你需要编写一个名为deletebooksfromdisplay
的方法来删除书籍。这个方法将接受一个书名作为参数,并从self.books
列表中删除该书名。这将使图书从库存中被移除。
然后,在borrowbooks
方法中,在书被借出后,你可以调用deletebooksfromdisplay
方法,以确保已借出的书不再在图书库中显示。这将防止同一本书被借出两次。
英文:
- I have 2 class Library, Student
- If book is in displaybooks function then book is issued
- if book book is already issued then it has to print
print(bookname + "NA/already taken")
Below is the code
class Library:
def __init__(self, listBooks):
self.books = listBooks
def displaybooks(self):
for book in self.books:
print(book)
def borrowbooks(self, bookname):
if bookname not in self.books:
print(bookname + "NA/already taken")
else:
print(bookname + " is issued for you")
# def deletebooksfromdisplay(self,bookname):
# self.books.remove(bookname)
class Student:
def requestbooks(self):
self.book = input("Enter name of the book you want to borrow: ")
return self.book
if __name__ == "__main__":
mylib = Library(
["invention", "rich&poor", "macroeconomics", "secret"])
student = Student()
print("""CHOOSE WHAT YOU WANT TO DO:-\n1. Listing all books\n2. Borrow books \n3.
Exit\n""")
while (True):
try:
usr_response = int(input("Enter your choice: "))
if usr_response == 1:
mylib.displaybooks()
elif usr_response == 2:
mylib.borrowbooks(student.requestbooks())
elif usr_response == 3:
print("THANK YOU ! \n")
exit()
break
else:
print("INVAILD INPUT! \n")
except Exception as e: #catch errors
print(f"{e}---> INVALID INPUT! \n")
My output is below
CHOOSE WHAT YOU WANT TO DO:-
1. Listing all books
2. Borrow books
3.
Exit
Enter your choice: 1
invention
rich&poor
macroeconomics
secret
Enter your choice: 2
Enter name of the book you want to borrow: secret
secret is issued
Enter your choice: 2
Enter name of the book you want to borrow: secret
secret is issued
You can see secret is issued
2 times Expected out is on second time it is print(bookname + "NA/already taken")
- Basically I need to write one function like below which has to delete from lisfofbooks
# def deletebooksfromdisplay(self,bookname):
# self.books.remove(bookname)
答案1
得分: 0
你可以在借阅后将书移除:
def borrowbooks(self, bookname):
if bookname not in self.books:
print(bookname + "NA/already taken")
else:
print(bookname + " 已借阅给你")
self.books.remove(bookname)
英文:
You can just remove the book after borrowing it:
def borrowbooks(self, bookname):
if bookname not in self.books:
print(bookname + "NA/already taken")
else:
print(bookname + " is issued for you")
self.books.remove(bookname)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论