Posts: 11
Threads: 2
Joined: Dec 2016
Jan-19-2017, 06:20 AM
(This post was last modified: Jan-19-2017, 06:56 AM by snippsat.)
Does anyone here knows how to do methods inside a class?
I need help to find:
1) a method to mark a book as completed
2 )a method to determine if the book is long (500 or more pages)
Like how do i write it?
Help is appreciated!
Update: i have wrote down the class. I just need that two method, but how do i write it?
class Book:
def __init__(self, title="", author="", pages=0, status=""):
self.title = title
self.author = author
self.pages = pages
self.status = status
def __str__(self):
return "{} by {}, total pages is {}.".format(self.title, self.author, self.pages)
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-19-2017, 07:36 AM
(This post was last modified: Jan-19-2017, 07:36 AM by j.crater.)
You already wrote two methods (init and str), although they are special class methods. But you proceed exactly the same way with writing your methods. Just give them proper names like mark_read() and is_long(), or anything you prefer.
I just stumbled upon this
https://www.python.org/dev/peps/pep-0008...-variables
you may use it to follow a naming convention for coding in Python.
Posts: 11
Threads: 2
Joined: Dec 2016
(Jan-19-2017, 07:33 AM)j.crater Wrote: You already wrote two methods (init and str), although they are special class methods. But you proceed exactly the same way with writing your methods. Just give them proper names like MarkRead() and IsLong(), or anything you prefer.
Yeah, i gotta write the methods under one class.
What do i have to write under the methods MarkRead() and IsLong()?
I'm a newbie on Python. ?
Posts: 1,150
Threads: 42
Joined: Sep 2016
Jan-19-2017, 07:44 AM
(This post was last modified: Jan-19-2017, 07:44 AM by j.crater.)
mark_read() and is_long() were my suggestion for method names, but you can use anything. "Under" the methods (in method body, better put), you should write the code that you want executed by calling a particular method. It works much like functions.
If you are new to Python and have not yet learned about functions, I suggest you dive into them before moving to classes and methods. Things will make a lot more sense then and your learning will be efficient.
https://www.tutorialspoint.com/python/py...ctions.htm
Posts: 2,953
Threads: 48
Joined: Sep 2016
Hm! Your class actually just holds a data. Why not a dictionary?
book = {'title': "", 'author': "", 'pages': ""}
print("{} by {}, total pages is {}.".format(book[titile], book[author], book[pages])
Posts: 8,163
Threads: 160
Joined: Sep 2016
(Jan-19-2017, 07:53 AM)wavic Wrote: Hm! Your class actually just holds a data. Why not a dictionary?
It is the Homework section. Obviously it's a OOP class, even if it looks strange to teach OOP when s/he is not familiar with more basic concepts.
Posts: 11
Threads: 2
Joined: Dec 2016
Jan-19-2017, 08:15 AM
(This post was last modified: Jan-19-2017, 08:22 AM by snippsat.)
(Jan-19-2017, 07:44 AM)j.crater Wrote: mark_read() and is_long() were my suggestion for method names, but you can use anything. "Under" the methods (in method body, better put), you should write the code that you want executed by calling a particular method. It works much like functions.
If you are new to Python and have not yet learned about functions, I suggest you dive into them before moving to classes and methods. Things will make a lot more sense then and your learning will be efficient.
https://www.tutorialspoint.com/python/py...ctions.htm
is this correct? :/
# a method to mark the book as completed
def mark_completed(self):
if self.status == "c"
self.title += "Mark as completed"
elif self.status == "r"
self.title += "Book is not completed"
# a method to determine if the book is long (500 or more pages)
def is_long(self):
if self.pages > 500:
return True
return False Edit admin use code tag.
Look at BBcode.
Posts: 8,163
Threads: 160
Joined: Sep 2016
this one
# a method to determine if the book is long (500 or more pages)
def is_long(self):
if self.pages > 500:
return True
return False is more or less correct, i.e. it could be done better, e.g.
# a method to determine if the book is long (500 or more pages)
def is_long(self):
return self.pages >= 500 as to this one:
# a method to mark the book as completed
def mark_completed(self):
if self.status == "c"
self.title += "Mark as completed"
elif self.status == "r"
self.title += "Book is not completed" I think you need to change status property and not change the book title.
Posts: 11
Threads: 2
Joined: Dec 2016
(Jan-19-2017, 08:25 AM)buran Wrote: this one
# a method to determine if the book is long (500 or more pages)
def is_long(self):
if self.pages > 500:
return True
return False is more or less correct, i.e. it could be done better, e.g.
# a method to determine if the book is long (500 or more pages)
def is_long(self):
return self.pages >= 500 as to this one:
# a method to mark the book as completed
def mark_completed(self):
if self.status == "c"
self.title += "Mark as completed"
elif self.status == "r"
self.title += "Book is not completed" I think you need to change status property and not change the book title.
how?
Posts: 8,163
Threads: 160
Joined: Sep 2016
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.
|