Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Methods
#11
(Jan-19-2017, 12:44 PM)buran Wrote: Both status and title are properties of class Book. In your code you change one of these properties and I just suggest that you should change the other one.

Is it like this?

# a method to mark the book as completed
def mark_completed(self):
if self.status == 'c':
return "Mark as completed"
elif self.status == 'r':
return "Book is not completed"
Reply
#12
You did not change status/title properties of the class, you have just made the method return a string.
Also, in your previous code, you used "+=" operator for changing status. That means appending a string to a variable, not changing it.
I would wholeheartedly recommend starting with Python basics, and working your way up to classes, if time allows it.
Reply
#13
(Jan-19-2017, 03:08 PM)j.crater Wrote: You did not change status/title properties of the class, you have just made the method return a string.
Also, in your previous code, you used "+=" operator for changing status. That means appending a string to a variable, not changing it.
I would wholeheartedly recommend starting with Python basics, and working your way up to classes, if time allows it.

omg, can anyone help me just type out what exactly should i type? i tried :(
Reply
#14
class Book:
   def __init__(self, title, author, pages, status='want to read'):
       self.title = title
       self.author = author
       self.pages = pages
       self.status = status
       
   def is_long(self):
       return self.pages>=500
       
   # as an alternative
   def is_long_descr(self):
       if self.pages>=500:
           return '{} is long book'.format(self.title)
       else:
           return '{} is not long book'.format(self.title)
           
   def mark_reading(self):
       print 'Start reading book {}'.format(self.title)
       self.status = 'currently reading'
   
       
   def mark_complete(self):
       print 'Finish reading book {}'.format(self.title)
       self.status = 'completed'

   def __str__(self):
      return "{} by {}, # pages: {}, status: {}.".format(self.title, self.author, self.pages, self.status)
     
#demo use 
my_book1 = Book(title='Alice in Wonderland', author='Lewis Caroll', pages=92)
my_book2 = Book(title='The Three Musketeers', author='Alexandre Dumas', pages=786, status='currently reading')
my_books = (my_book1, my_book2)

for book in my_books:
   print book
   print 'Is long: {}'.format(book.is_long())
   print book.is_long_descr()
   if book.status not in ('currently reading', 'completed'):
       book.mark_reading()
   book.mark_complete()
   print book
   print '\n----------------\n'
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020