class Book():
def __init__(self, listOfBooks):
self.availableBooks = listOfBooks
def displayAvailableBook(self):
print()
self.displayAvailableBook()
class TextBooks(Book):
def __init__(self,listOfBooks):
super().__init__(listOfBooks)
def displayAvailableBook(self):
print()
print("Available Text Books: ")
print("--------------------")
for book in self.availableBooks:
print(book)
print()
class ReferenceBooks(Book):
def __init__(self,listOfBooks):
super().__init__(listOfBooks)
def displayAvailableBook(self):
print()
print("Available Reference Books: ")
print("-------------------------")
for book in self.availableBooks:
print(book)
print()
def main():
book = Book()
t_books = TextBooks(['Rescued by Python','Introduction to AI', 'Machine Learning using Python'])
r_books = ReferenceBooks(['Dictionary','Bible','Encyclopidia'])
while True:
print("Enter 1 to display the available text books")
print("Enter 2 to display the available reference books")
print("Enter 3 to request for a book")
print("Enter 4 to return a book")
print("Enter 5 to exit")
userChoice = int(input())
if userChoice is 1:
book.displayAvailableBook()
elif userChoice is 2:
r_books.displayAvailableBook()
elif userChoice is 3:
requestedBook = customer.requestBook()
t_books.lendBook(requestedBook)
elif userChoice is 4:
returnedBook = customer.returnBook()
t_books.addBook(returnedBook)
elif userChoice is 5:
quit()
main()
答案 0 :(得分:2)
您不会将参数传递给您的班级Book():
def main():
book = Book()
这是一个问题,因为 init 必须由代码中名为listOfBooks的一个参数提供。因此,要解决您的问题,请为Book构造函数提供名称:
def main():
bookList = ['a','b','c']
book = Book(bookList)
答案 1 :(得分:0)
该代码中存在一些问题: 1.)如果不提供书籍列表,则无法初始化类对象书。 2.)您永远不会初始化对象客户 3.)未定义类函数requestBook(),lendBook()和addBook()。
这是一个修改版本,我认为你做了你想要的但是我无法解决问题2.)和3.)因为我不知道你打算在那里做什么
class Book():
def __init__(self, listOfBooks):
self.availableBooks = listOfBooks
def displayAvailableBook(self):
print()
self.displayAvailableBook()
class TextBooks(Book):
def __init__(self,listOfBooks):
super().__init__(listOfBooks)
def displayAvailableBook(self):
print()
print("Available Text Books: ")
print("--------------------")
for book in self.availableBooks:
print(book)
print()
class ReferenceBooks(Book):
def __init__(self,listOfBooks):
super().__init__(listOfBooks)
def displayAvailableBook(self):
print()
print("Available Reference Books: ")
print("-------------------------")
for book in self.availableBooks:
print(book)
print()
def main():
#book = Book() This does not work since no list provided
t_books = TextBooks(['Rescued by Python','Introduction to AI', 'Machine Learning using Python'])
r_books = ReferenceBooks(['Dictionary','Bible','Encyclopidia'])
while True:
print("Enter 1 to display the available text books")
print("Enter 2 to display the available reference books")
print("Enter 3 to request for a book")
print("Enter 4 to return a book")
print("Enter 5 to exit")
userChoice = int(input())
if userChoice is 1:
t_books.displayAvailableBook()
r_books.displayAvailableBook()
elif userChoice is 2:
r_books.displayAvailableBook()
elif userChoice is 3:
requestedBook = customer.requestBook()
t_books.lendBook(requestedBook)
elif userChoice is 4:
returnedBook = customer.returnBook()
t_books.addBook(returnedBook)
elif userChoice is 5:
quit()
main()
答案 2 :(得分:-1)
你得到的错误很可能是来自第38行的TypeError
。从我所看到的,这是因为你遗漏了创建图书对象的参数(book = Book()
)。
在您的课程定义中,您已经完成了这项工作,因此您需要传递一系列书籍,这些书籍尚未包含在对象的创建中。