Apr-10-2018, 11:58 AM
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:
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 >>>