Creating class methods


#1

Thanks for your help. Now moving to the next section with another problem here is the code that is not working. So please help and I will really do appreciate it if you explained when exactly I should add the self key word.

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def display_car(self,model,color,mpg):
        return "This is a [color] [model] with [mpg] MPG."

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

print my_car.display_car(model,color,mpg)

Referring to member variables
#2

Hi @coursejumper28708,

Like other methods within a class definition, the display_car method that you are writing already has access to Car's instance variables, such as model, color, and mpg. Therefore, you do not need to supply them as parameters in the method header. Modify the method header as follows, so that it only has a self parameter ...

    def display_car(self):

Then you can use self to access the instance variables within the method.

You need to modify this statement, so that in the output, what you currently have as square brackets containing variable names is replaced by the actual values of the instance variables ...

        return "This is a [color] [model] with [mpg] MPG."

Give it a try and post your code if you encounter any problems.


#3

Still cannot use the instances here is the code.

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = str(mpg)
    def display_car(self,model,color,mpg):
        
        return "This is , %s, , %d, model a with , %f, MPG."%(my_car.color,my_car.model,my_car.mpg) 
my_car = Car("DeLorean", "silver", 88)

print my_car.display_car("dsgdg","sdfgsdg",88)

#4

Hi @coursejumper28708,

The mpg property should be stored as a number rather than string, so within the __int__ method, change this ...

        self.mpg   = str(mpg)

... to this ...

        self.mpg = mpg

For reasons discussed above, the header of the display_car method should be changed to ...

    def display_car(self):

The line of code that it contains also needs some additional work. Output the three appropriate properties as strings. To do so, you will need to convert self.mpg to a string, and use the %s format specifier to output each of the three properties. Accordingly, you can do this inside the display_car method ...

        return "This is a %s %s with %s MPG."%(self.color, self.model, str(self.mpg))

Now, with the header and contents of the display_car method having been refined appropriately, you can instantiate a Car and display its information, as follows ...

my_car = Car("DeLorean", "silver", 88)
print my_car.display_car()

#5

It worked that is awesome thank you so much.

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def display_car(self):
        
        return "This is a %s %s with %s MPG."%(self.color,self.model,self.mpg) 
my_car = Car("DeLorean", "silver", 88)
print my_car.display_car()

#6