Referring to member variables


#1

Can you please tell me why this code is not working ?

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg = mpg
my_car = Car
my_car.model = "DeLorean"
my_car.color = "silver"
my_car.mpg = 88   
print my_car.model
print my_car.color
print my_car.mpg

#2

Hi @coursejumper28708,

Here, you need to supply the three appropriate arguments within parentheses, as you instantiate a Car ...

my_car = Car

#3

I modified it and it didn't work please help.

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg = mpg
my_car = Car(self.model,self.color, self.mpg)
my_car.model = "DeLorean"
my_car.color = "silver"
my_car.mpg = 88   

print my_car.model
print my_car.color
print my_car.mpg

#4

You do not need these three statements, so they can be removed ...

my_car.model = "DeLorean"
my_car.color = "silver"
my_car.mpg = 88

Modify this statement to pass, as arguments, the three values specified in the instructions, namely "DeLorean", "silver", and 88 ...

my_car = Car(self.model,self.color, self.mpg)

#5

I thought I need them in the section right before this one but now i've realized that it should be like this

my_car = Car("DeLorean","silver",88)

Any way,back to our section .Thank you so much for you help I modified it again and it worked here is the code

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg = mpg
my_car = Car("DeLorean","silver",88)
print my_car.model
print my_car.color
print my_car.mpg

#6

Good, then, as long as you understand it now. :smile: Best of luck with the upcoming exercises.


#7

A post was split to a new topic: Creating class methods


#8