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 __init__

ellie adam
ellie adam
26,376 Points

create a class name

class Student:
  name = emma
  def__init__(self, name)
  self.name = emma

can't solve it.

2 Answers

Guled Ahmed
Guled Ahmed
12,806 Points

Hello again Ellie,

Your code looks very good, but there is a slight problem. The assignment says to give the name parameter a default value of your choice. So in the init method, you just need to set name to whatever you want. This ensures that if the user does not enter a name, there will always be a default as a backup. Then finally, you need to set the self.name to what the parameters name is. So in this case, when I make a student object like: guled = Student("Guled"), self.name will equal Guled. Also emma needs to be a string, so do not forget to put parenthesis around it.

class Student:
  name = "Guled"
  def __init__(self,name="Guled"):
    self.name = name
ellie adam
ellie adam
26,376 Points
class Student:
  name = emma
  def__init__(self.name)
  self.name = emma

still don't get it :(

One more question How come your code is colored and mine is gray?

Guled Ahmed
Guled Ahmed
12,806 Points

Hello Ellie,

I'll explain how to accomplish this task one step at a time. First we'll start with your name variable. The assignment wants you to assign a name to the name variable. A name is a string of characters, you placed the name emma for the name variable to store, but remember emma is a name, which is a string of characters. In order to represent emma as a string of characters you must place parenthesis around the name. So it should look like this:

class Student:
  name="emma"

This will ensure that the name variable will store a string named emma. Otherwise the assignment will assume you are trying to store an object, or variable into the variable name which is not what we want.

Next, we need to create the init method, with two parameters: self and name. We need the self parameter, this is needed to create new objects. We also need a name parameter, but remember that the assignment says to give it a default value, so it should look like this so far:

class Student:
  name = "emma"
  def __init__(self, name="Some Person")

Also I noticed you had no space between 'def' and 'init', there needs to be a space between them

Next, we need to set our name attribute, the one that has emma, to whatever the new object is called, so the final code should look like this:

class Student:
  name = "emma"
  def __init__(self , name="Some Person")
  self.name = name

Now, I think you want to why we did self.name=name, This just means that whenever we create a new Student their name will be whatever we set it to be. Here is an example:

I'll make a new student named "Guled". So here is how I'd do it:

guled = Student("Guled")

Now, instead of the student having the default name of emma or Some Person it will be Guled, because self.name (which was emma) will be set to whatever I passed in to the init function.

If I were to create a new student without putting a name in like this:

guled = Student()

My student guled will have the name Some Person because I did not give it a name, so the default kicked in.

I hope this helped. The final code is down below. If this still doesn't make sense, please review the the previous videos again to see if that will help:

class Student:
  name = "emma"
  def __init__(self , name="Some Person")
  self.name = name
Guled Ahmed
Guled Ahmed
12,806 Points

To make your code have color click on the Markdown Cheatsheet which shows up when start making a post. It should read: Reference this Markdown Cheatsheet for syntax examples for formatting your post.

ellie adam
ellie adam
26,376 Points

Thanks for explaining in detail but I got this error. Bummer! SyntaxError: def init(self , name="Some Person")

I typed all step by step code. I did watch video few times.

class Student:
  name = "emma"
  def __init__(self , name="Some Person")
  self.name = name
Guled Ahmed
Guled Ahmed
12,806 Points

You need to be careful about indentation. Remember that there needs be an indent after you create a method. Python has strict rules about this. Try this:

class Student:
  name = "emma"
  def __init__(self,name="Some Person"):
    self.name = name

Notice, self.name=name has been indented within the init method.

I hope this helps.

ellie adam
ellie adam
26,376 Points

Thank you so much for sticking with me and helping out. It didn't pass with emma so I used your code then finally passed. thanks again :)

class Student:
  name = "Guled"
  def __init__(self,name="Guled"):
    self.name = name
ellie adam
ellie adam
26,376 Points

I always try small tilda before and after the code. But always it turn out gray and it is really hard to read. I googled but couldn't solve this mystery!

Guled Ahmed
Guled Ahmed
12,806 Points

I believe a tilda is this --> ~ , Try making three of these --> `

Then type the name of the programming language next to it, then put the code, then put another 3 of these --> ``` under it. I can't type it out since the site will turn whatever I do into code.