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 trial

Python

Hi!, I need to start the car in color red and afterwards I have to change it to blue, what is the problem in this code?

class Car():

def __init__(self, brand, color):
    self.brand = brand
    self.color = color
    self.engine_started = False

def start_engine(self):
    self.engine_started = True

def stop_engine(self):
    self.engine_started = False

# Add a method below to change the color of a car
def ch_color(self, color):
    self.color = color

Instantiate a red car and change it to blue

car_red = Car('brand', 'red')


car_red.__initiate__(self, 'brand', 'red')

car_blue = car_red.ch_color('brand', 'blue')

I think I got it!

Is the following code correct?

class Car():

def __init__(self, brand, color):
    self.brand = brand
    self.color = color
    self.engine_started = False

def start_engine(self):
    self.engine_started = True

def stop_engine(self):
    self.engine_started = False

# Add a method below to change the color of a car
def ch_color(self, color):
    self.color = color

Instantiate a red car and change it to blue

car_red = Car('brand', 'red')

car_red.init('brand', 'red')

car_blue = car_red.ch_color('blue')

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Pro tip: you can improve post readability by wrapping code in

```python and ```

using backticks (left of 1 on keyboard)

1 Answer

When you run car_red = Car('brand', 'red'), it runs the __init__ method in Car for you. When you run car_blue = car_red.ch_color('blue'), car_red changes its color to 'blue', then returns None, and car_blue is set to None.

car_red = Car('brand', 'red')
car_red.ch_color('blue')

Thanks jb !

I change the code for the following, how can I change the return of None that you mention? In this code I tried to see if the blue_car is an instance of the class and return false, if it should be equal to red why is not part of the same class?

class Car():

def __init__(self, brand, color):
    self.brand = brand
    self.color = color
    self.engine_started = False

def start_engine(self):
    self.engine_started = True

def stop_engine(self):
    self.engine_started = False

# Add a method below to change the color of a car
def ch_color(self, color):
    self.color = color

Instantiate a red car and change it to blue

car_red = Car('brand', 'red') car_blue = car_red.ch_color('blue')

print(isinstance(car_blue, Car))

If you want ch_color to have a return value,

def ch_color(self, color):
    self.color = color
    return self