我已经创建了一个python程序,在该程序中,客户可以从库中归还图书和借阅图书,同时执行I get error*TypeError:borrow()缺少1个必需的位置参数:“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()
创建
Customer
类的实例,不要直接使用该类:您的代码中有一些错误:
self.x
不是类的属性。你可以直接写x
李>availablebook
变量作为init函数的输入Library
和Customer
类,因此缺少了一个参数。如果你考虑添加^ {CD3}},你可以写^ {CD6}},否则只写^ {CD8}}。<李>我认为最好是在循环之前创建一个库:
my_lib = Library([])
然后在Customer
函数中添加一个库输入,以便编辑所需的库,从而避免每次创建新库以下是我建议您使用的代码:
availablebook
应该是__init__
函数中的列表另外,修改
while
循环相关问题 更多 >
编程相关推荐