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 trialAshley Keeling
11,476 PointsI don't get instances(class challenge)
I have looked at the teacher notes and I have tried to do but it wont work
class Student:
name="Ashley"
me.student(name)
print(me)
5 Answers
Jakob Hansen
13,746 PointsOkay I did the challenge myself now, and it should look like this:
class Student:
name = "Ashley"
me = Student()
print(me.name)
Jakob Hansen
13,746 PointsThink of a class as a blueprint or a recipe, it's not the actual cake, you can't eat the recipe ;).
You have to instantiate the class, to use an instance of it. To create an instance of a class, you would do this considering you had a class called Student
class Student: name = "Ashley"
me = Student.name print(me)
You use dot notation to acces properties inside the class :) dot notation being "Student.name"
Ashley Keeling
11,476 PointsI still don't get what I have done wrong, what am I missing in the code
AJ Salmon
5,675 PointsThis is a really good analogy!
Jakob Hansen
13,746 PointsAlright, so we can give a class attributes, in this case, name is an attribute, we assign it a default value ( your name in this case)
class Student:
name = "Ashley"
then to instantiate the class, we first need to create a variable to hold this instance of the class. you have got the right idea, in your code, but the indentation is making Python think that
me.student(name)
print(me)
is meant to be inside the class declaration, which it's not.
so, the problem is indentation, and then using dot notation to acces the .name attribute of your Student class, instead of student(name) :) I hope this helps
Ashley Keeling
11,476 Pointsi have done this and it is saying ,task 1 is no longer passing :
class Student:
name="Ashley"
me=student.name
print(me)
Jakob Hansen
13,746 PointsThat's because
class Student:
name="Ashley"
name = "Ashley" is no longer indented. Which means it's not part of the Student class declaration.
it's supposed to look like this:
class Student:
name="Ashley" #This being indented, so Python know's it is INSIDE the class declaration
me=student().name
print(me)
Jakob Hansen
13,746 PointsAlso, when you're trying to instantiate the class, lower and upper case, matters :)
me = Student.name
It's probably just a typo :)
Ashley Keeling
11,476 Pointsthe code is working in python but not in the challenge , it is saying :
Bummer! me
doesn't seem to be an instance of Student
Jakob Hansen
13,746 PointsYou haven't capitalized the S
me = student.name
you are trying to acces the name attribute inside of the Student class. your class is called Student (with capital S) not student
Ashley Keeling
11,476 Pointsin the challenge I used a capital s but when I wrote it out on the comment , I missed the capital letter , and it is still saying
me
doesn't seem to be an instance of Student
Jakob Hansen
13,746 PointsJakob Hansen
13,746 PointsApparently the treehouse challenge, wants you to split it across 2 lines, i thought it was simpler to just group it. We just create an instance of Student, and then first acces the name attribute when printing it to the console.
Ashley Keeling
11,476 PointsAshley Keeling
11,476 PointsTHANK YOU I passed the challenge, thanks for all of the help