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 Instant Objects Your first class

What is wrong with my code. Someone help please. Been stuck for too long

Challenge Task 2 of 2 make an instance of your class named me. Then print() out the name attribute. Its not working

class Student:
    name = "Tino"
    me = Student()
Jeff Muday
Jeff Muday
Treehouse Moderator 26,999 Points

You can fix your scenario with simple indentation changes. Python is very sensitive to indentation as it denotes the block level of code.

class Student:
    # here we are defining a class
    name = "Tino" # name is a PROPERTY of Student

# the next two statements are NOT indented because these are at the top level
me = Student() # here we "instantiate" me as a Student() class object
print(me.name) # here we access the PROPERTY name

2 Answers

Fred Gablick
Fred Gablick
5,840 Points

You're thinking of this as a function instead of an object class. You need to define the object outside of the class. This is quick and dirty, but I think you'll see it:

>>> class Student:
...     pass
>>> me = Student()
>>> me.nickname = "Bonehead"
>>> print(me.nickname)

You can use an init function inside the class to define your attributes, but you still want to define the object outside of the class.

>>> class Student:
...     def __init__(self, name):
...             self.name = name
>>> me = Student("Bonehead")
>>> print(me.name)

I hope that helps.

The problem was with indentation. Thank you all