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

Why is this code failing

Challenge task 2 of 2 and the error is it can't find 'Student'.My code is attached :)

first_class.py
class Student:
    name = 'Aizah'
    Student('me')
    print(name)

4 Answers

Steven Parker
Steven Parker
211,043 Points

The code after the class should not be indented. Also, you may want to review the syntax for creating an instance and accessing attributes. Creating an instance will require the assignment operator ("_"), and accessing an attribute will use the membership operator (".").


UPDATE: - the (task 2) code after the class is still indented

  • there is still no creation of the variable "me" and assignment with a new class instance
  • there is still no reference to the class in the "print", and no membership operator
  • plus now, the class name is being referred to as "Student_me" instead of "Student"

You might want to review the video, and take another look at the examples in it.

Steven Parker
Steven Parker
211,043 Points

Another update:

  • line 2 does need to be indented, it's part of the class
  • line 3 needs parentheses after the class name to create a new instance
  • line 4 should reference the instance name instead of the class itself
class Student:
    name = 'Aizah'
    Student_me()
    print(name) 

it is still not running

ok, thank you

Sorry to bother you but where am I going wrong now

class Student:
name = 'Aizah'
me = Student
print(Student.name)