Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Objects Create a Class with a Method

Y. Kravets
Y. Kravets
10,350 Points

Hi guys! Is the problem with the .format?

Hi :-) I am trying to complete the challenge with Object oriented Python and I seem to be stuck with the open close hours method. Could anyone please have a look at the code and explain what is it I am doing wrong? Thanks in advance

method.py
class Store:
  open = '9'
  close = '18'

  def hours(self):
    print("We're open from {} to {}.".format(self.open,self.close))

4 Answers

Thomas Kirk
Thomas Kirk
5,246 Points

As I understand it (only a beginner myself):

print just displays data on the screen, for the user's benefit

return creates a value that can be used elsewhere later (assigned to variables, used as an argument in other functions, etc)

Consider the two functions below:

def print_func():
    print(2)

def return_func():
    return 2

return_func(), which returns a value of 2, can now be used however we want, for example:

>>> 4 * ret_func()
8

while print_funct() will print 2 but, having a value of None, will give an error if we try to use it in the way we did for return_func().

>>> 4 * print_func()
2
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    4 * print_func()
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

Hope that helps!

Thomas Kirk
Thomas Kirk
5,246 Points

Hi,

The question asks you to return that string of text. You need to use return as oppose to print.

Y. Kravets
Y. Kravets
10,350 Points

Thanks Thomas! Is it possible to ask for a simple explanation of the difference between using print and return? I don't think I understand it all that well and I could use a clarification.

Thanks!

Y. Kravets
Y. Kravets
10,350 Points

Thank you! I think I finally get the point :)