![]() |
University Assignment Help needed - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: University Assignment Help needed (/thread-9464.html) |
University Assignment Help needed - Diovanno - Apr-10-2018 Hi All, I'm a first year student studying Informatics at The University of South Africa and one of my modules is introduction to Python. so far I've been teaching myself Python (as the course is part-time and there is no tutors available for part-time studies) but am currently stuck with one of my assignment questions. the assignment questions goes as follows: Create a class Publication with public member variables publisher, title and price with the following specifications: (18) • Add init() method of the class that initialises string member variables to empty strings and numeric values to 0. • Add two more methods to the class: populate() and display(). o The populate() method is used to assign values to the member variables of the class. o The display() method is used to display the member variables of the class. • Derive a class Book from Publication. o The class Book has two public member variables of its own: ISBN and authorname. o Define the init() method for the class Book that initialises string member variables to empty strings and numeric values to 0, and also override the populate() and display() methods of the base class. o Create an instance bookObj of the class Book in a main program. • The main program should then prompt the user to enter the following values: Publisher : Romfort Title : Computing for beginners Price: 280 ISBN: 123456 Author Name: Jo Mahlangu • The above attributes should be assigned to the instance bookObj using its populate() method. o Then display all these attributes back to the console using the display() method of the instance bookObj A sample run: Enter publisher name: Romfort Enter title: Computing for beginners Enter price: 280 Enter ISBN number: 123456 Enter author name: Jo Mahlangu The details of the book are: Publisher: Romfort Title: Computing for beginners Price: R280.00 ISBN: 123456 Author Name: Jo Mahlangu This is the code i have so far: import pickle class publication: def __init__(self,publisher,title,price): self.publisher=publisher self.title=title self.price=price def populate(self,publisher,title,price): self.publisher=publisher self.title=title self.price=price def display(self): print('Publisher:',self.publisher) print('Title:',self.title) print('Price:R',self.price) class book(publication): def __init__(self): publication.__init__(self) self.ISBN=ISBN self.Authorname=Authorname f=open('Info.bin','wb') p=(input('Enter Publisher Name:')) t=(input('Enter Title:')) pr=(input('Enter Price:')) i=(input('Enter ISBN number:')) a=(input('Enter Author Name:')) bookObj=book(p,t,pr,i,a) pickle.dump(bookObj,f) f.close() print('\nThe Details Of The Book Are:') f=open('Info.bin','rb') while True: try: bookObj=pickle.load(f) except EOFError: break else: bookObj.book() f.close()Can someone please tell me where i am going wrong? as the above asks to enter the details but when it comes to displaying the info back to me i get the error: line 35, in <module> bookObj=book(p,t,pr,i,a) TypeError: __init__() takes 1 positional argument but 6 were given >>> RE: University Assignment Help needed - mlieqo - Apr-10-2018 The error is because when you are trying to create your bookObj: bookObj=book(p,t,pr,i,a)you are giving it too many arguments, but based on your init method it has only 1 argument 'self' class book(publication): def __init__(self): publication.__init__(self)Also In your assingment it is stated, that your init() methods should initialize string member variables to empty strings and numeric values to 0, and your Book class should also have overriding populate() and display() methods which you are missing. So together with everything else I got something like this(Hopefuly you are working with python 3.6, otherwise the print statements in display() methods are not going to working for you): class Publication: def __init__(self): self.publisher = '' self.title = '' self.price = 0 def populate(self, publisher, title, price): self.publisher = publisher self.title = title self.price = price def display(self): print(f'Publisher: {self.publisher}') print(f'Title: {self.title}') print(f'Price: R{self.price:.2f}') # formatted to display 2 decimals class Book(Publication): def __init__(self): Publication.__init__(self) self.ISBN = 0 self.authorname = '' def populate(self, publisher, title, price, ISBN, authorname): Publication.populate(self, publisher, title, price) self.ISBN = ISBN self.authorname = authorname def display(self): Publication.display(self) print(f'ISBN: {self.ISBN}') print(f'Authorname: {self.authorname}') publisher = input('Enter Publisher Name: ') title = input('Enter Title: ') price = int(input('Enter Price: ')) isbn = int(input('Enter ISBN number: ')) author = input('Enter Author Name: ') bookObj = Book() # here you create Book object -> initialise the variables either as empty string or 0 bookObj.populate(publisher, title, price, isbn, author) # here you rewrite these empty strings and 0 to values from the inputs print('The Details Of The Book Are:') bookObj.display() # and this method displays all the values RE: University Assignment Help needed - Larz60+ - Apr-10-2018 first, post code that matches error. error shows line 35, but the code that caused the error above is on line 33. errors must match actual code. That said, you are instantiating a copy of 'book' with p, t, pr, i and a the __init__ from book does not accept any arguments: class book(publication): def __init__(self):You must allot placeholders for these items, which must have the same name as the variables used within the class, for example: class book(publication): def __init__(self, publisher, title, price, ISBN, author): RE: University Assignment Help needed - Diovanno - Apr-10-2018 Thanks Mlieqo, i can see where i went wrong now, i was just complicating things in my original code. It's tough trying to teach one's self a programming language but your explanations in your reply really helped me understand what was needed and for future reference. Thanks again. |