在类中如何调用函数内的函数

huangapple go评论114阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年3月9日 22:25:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定