Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 :)