Sunday, 25 August 2013

Private and Public attributes using Python

Private and Public attributes using Python

I found this question in my Python book but Im having difiiculties to
print the author, title and number of copies using the private and public
attributes.
Here is the instructions:
class Book(object):
def __init__(self,author,title,copies):
#The __init__ method must accept arguments for
#the author (private attribute)
#the title (public attribute) and
#the number of copies (private atrribute)
def display(self):
#The display method must display the information
#for the book, that is the author, title and number of copies
#available
def sell_book(self,sold):
#This method must extract from the number of copies available
#the number of copies sold.
#Test that the user doesn't sell more copies than there are
#available.
#Display a suitable message if it was the last copy sold
def main():
#create an instance of the Book class
myBook = Book(author="JB Wellington", title="The digital divide",
copies=40)
myBook.sell_book(sold=3)
myBook.display()
#main
main()
Here is my code: class Book(object): def init(self, author, title,
copies): self._author = author self.title = title self._copies = copies
def display(self):
print "author:", self.__author, "\n"
print "Tilte:", self.titile, "\n"
print "Number of copies:", self.__copies, "\n"
def sell_book(self, sold):
if self.__copies < 0
print"The last copie sold"
def main():
#create an instance of the Book class
myBook = Book(author="JB Wellington", titile="The digital divide",
copies = 40)
myBook.sell_book(sold=3)
myBook.display()
#main
main()

No comments:

Post a Comment