Reputation: 23
I have a class object and tried to add that object to a class list but it doesn't work for some reason. What am I doing wrong here? I am new to Python. I know this kind of question would not fit in this community but I do need help from professional people like you guys. It would be appreciated if someone can help solve this! Thanks.
Here's my code snippet below:
class Catalogue:
def __init__(self, catalogue_list):
self._catalogue_list = catalogue_list
def add_item(self):
item = LibraryItemGenerator.create_item()
self._catalogue_list.append(item) # append not working?...
print(item)
def display_available_items(self):
print("Item List")
print("--------------", end="\n\n")
for library_item in self._catalogue_list:
print(library_item)
class LibraryItemGenerator:
@staticmethod
def create_item():
print("What kind of items would you like to add to the library catalogue?")
print("1. Book")
print("2. DVD")
print("3. Journal")
option = int(input("Select type of item: "))
title = input("Enter title: ")
call_num = input("Enter call number: ")
author = input("Enter author name: ")
num_copies = input("Enter the number of copies: ")
if option == 1:
return Book(call_num, title, num_copies, author)
if option == 2:
release_date = input("Enter release date: ")
region_code = input("Enter region code: ")
return Dvd(call_num, title, num_copies, author, release_date, region_code)
if option == 3:
names = input("Enter name: ")
issue_number = input("Enter issue number: ")
publisher = input("Enter publisher: ")
return Journal(call_num, title, num_copies, author, names, issue_number, publisher)
def main():
book = Book("263.25A", "Burgers and Yam Fries", 3, "Dr. Hou")
journal = Journal("267.21B", "I am hungry!", 5, "Dr. Hou", "THE NEWYORK TIMES", 21, "James Poul")
dvd = Dvd("193.25C", "Overwatch is fun", 2, "Dr. Hou", "2020-06-23", "CA")
cat = Catalogue([book, journal, dvd])
cat.add_item()
cat.display_available_items() #doesn't show the newly created objects.. append() didn't work?
if __name__ == '__main__':
main()
Upvotes: 1
Views: 744
Reputation: 76
suggest using pdb debugger. just insert pdb.set_trace() between
item = LibraryItemGenerator.create_item()
and self._catalogue_list.append(item)
. Use commands to navigate what actually happen before and after calling append().
in pdb mode, you can do print(ietm, self._catalogue_list)
and try self._catalogue_list.append(item)
. Then after calling self._catalogue_list.append(item)
, print(self._catalogue_list)
again. compare it with your previous print [<Labs.Lab3.book.Book object at 0x10840bfd0>, <Labs.Lab3.journal.Journal object at 0x10840bf70>, <Labs.Lab3.dvd.Dvd object at 0x1084072e0>]
also i would suggest if..elif..elif instead of if..if..if. Former will raise error where you can catch if option not in [1, 2, 3]
Upvotes: 1
Reputation: 5479
Here is a fully functional code with all of the classes and methods fixed:
class Catalogue:
def __init__(self, catalogue_list):
self._catalogue_list = catalogue_list
def add_item(self, item):
self._catalogue_list.append(item)
def display_available_items(self):
print("Item List")
print("--------------", end="\n\n")
for library_item in self._catalogue_list:
print(library_item)
class LibraryItemGenerator:
def __init__(self, option):
self.option = option
self.title = input("Enter title: ")
self.call_num = input("Enter call number: ")
self.author = input("Enter author name: ")
self.num_copies = input("Enter the number of copies: ")
if option == 1:
pass
elif option == 2:
self.release_date = input("Enter release date: ")
self.region_code = input("Enter region code: ")
elif option == 3:
self.names = input("Enter name: ")
self.issue_number = input("Enter issue number: ")
self.publisher = input("Enter publisher: ")
def __str__(self) -> str:
#Provides a readable description of the object
description = f'Title: {self.title}, ' \
f'Call No: {self.call_num}, ' \
f'Author: {self.author}, ' \
f'Number of Copies: {self.num_copies}'
if self.option == 1:
return "BOOK: " + description
elif self.option == 2:
description += f', Release date: {self.release_date}, ' \
f'Region code: {self.region_code} '
return "DVD: " + description
elif self.option == 3:
description += f', Names: {self.names}, ' \
f'Issue number: {self.issue_number}, ' \
f'Publisher: {self.publisher}'
return "JOURNAL: " + description
def main():
cat = Catalogue([])
while True:
print("What kind of items would you like to add to the library catalogue?")
print("1. Book")
print("2. DVD")
print("3. Journal")
option = int(input("Select item type (or -1 to finish): "))
if option == -1:
break
item = LibraryItemGenerator(option)
cat.add_item(item)
cat.display_available_items()
if __name__ == '__main__':
main()
After querying the user, this is a sample output:
BOOK: Title: Odyssey, Call No: 23, Author: Homer, Number of Copies: 2
DVD: Title: Total Recall, Call No: 00234, Author: Picture Company, Number of Copies: 2, Release date: 1980, Region code: 908
Upvotes: 0