【问题标题】:Error while calling python function, TypeError: returnbook() missing 1 required positional argument: 'self'调用 python 函数时出错,TypeError:returnbook() 缺少 1 个必需的位置参数:'self'
【发布时间】:2020-08-06 23:14:37
【问题描述】:

我创建了一个 python 程序,客户可以在其中返回书籍并从图书馆借书,但在执行时出现错误 * TypeError: borrow() missing 1 required positional argument: 'self' *

我应该做哪些改变才能成功执行程序?

我会先调用 returnbook() 函数,因为图书馆目前没有书。


class Library:
    def __init__(self):
        self.availablebook = availablebook
    def reducebook(self,book):
        if book in self.availablebook:
            self.availablebook.remove(book)
            print('book is removed')
    def addbook(self,book):
        self.availablebook.append(book)
        print('book added')

class Customer:
    def borrow(self):
        print('enter book')
        book = input()
        Library.reducebook(book)
    def returnbook(self):
        print('enter book')
        book = input()
        Library.addbook(book)

while True:
    print('enter 1 for add book,2 for borrow book,3 to exit')
    self.x = int(input())
    if(x==1):
        Customer.borrow()
    elif(x==2):
        Customer.returnbook()
    else:
        print('exiting')
        quit()


【问题讨论】:

标签: python


【解决方案1】:

创建Customer类的instance,不要直接使用该类:

customer = Customer()
customer.borrow()
customer.returnbook()

【讨论】:

    【解决方案2】:

    availablebook 应该是__init__ 函数中的列表。

    self.availablebook = []
    

    另外,修改您的 while 循环。

    while True:
        print('enter 1 for add book,2 for borrow book,3 to exit')
        x = int(input())
        if(x==1):
            Customer().borrow()
        elif(x==2):
            Customer().returnbook()
        else:
            print('exiting')
            quit()
    

    【讨论】:

      【解决方案3】:

      你的代码有一些错误:

      1. self.x 不是类的属性。你可以写x
      2. 您必须添加 availablebook 变量作为 init 函数的输入
      3. 你得到一个缺少的参数,因为你没有创建你的LibraryCustomer 类的好方法。如果您考虑添加availablebook 输入,您可以写Library([]).borrow(),否则只需写Library().borrow()

      我认为最好在循环之前创建一个库:my_lib = Library([]) 然后在 Customer 函数中添加库输入,以便编辑所需的库,从而避免每次都创建新库。

      这是我建议你的代码:

      class Library:
          def __init__(self, availablebook):
              self.availablebook = availablebook
      
          def reducebook(self, book):
              if book in self.availablebook:
                  self.availablebook.remove(book)
                  print('book is removed')
      
          def addbook(self,book):
              self.availablebook.append(book)
              print('book added')
      
      class Customer:
          def borrow(self, library):
              print('enter book')
              book = input()
              library.reducebook(book)
      
          def returnbook(self, library):
              print('enter book')
              book = input()
              library.addbook(book)
      
      my_lib = Library([])
      while True:
          print('enter 1 for add book,2 for borrow book,3 to exit')
          x = int(input())
      
          if(x==1):
              Customer().borrow(my_lib)
      
          elif(x==2):
              Customer().returnbook(my_lib)
      
          else:
              print('exiting')
              quit()
      

      【讨论】:

        猜你喜欢
        • 2020-10-19
        • 1970-01-01
        • 1970-01-01
        • 2021-12-15
        • 2023-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多