7. Creating Class Methods - strange error. Please help!


#1

What is printed out is:
This is a silver DeLorean with 88 MPG.
None

The code is:
class Car(object):

Creates a new class called "Car"

condition = "new"

Creates a variable called condition that is = to "new"

def __init__(self, model, color, mpg):
    self.model = model
    self.color = color
    self.mpg = mpg

Puts model, color, and mpg within the class

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)

Creates a new instance of the class called "my_car"

Makes model = DeLorean, color = silver, and mgp = 88

print my_car.display_car()

But I get this error message:
Oops, try again. The only place the actual values of my_car should appear is when it is created; always use member variables to access these values.

What is wrong???


#2

Hi,

Just delete this comment:

And it should work fine :smiley:


#3

That is a comment like the others... i.e.: # Makes.... So has nothing to do with the code.


#4

Try it yourself and you will see..


#5

I did... it worked. WHY????


#6

Because the editor doesn't ignore the comments, but sees them as normal strings, I think. So if you have the values somewhere else, it will ignore the # and sees the values twice.


#7
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' + ' ' + self.color + ' ' + self.model\
        + ' ' + 'with' + ' ' + str(self.mpg) + ' ' + 'MPG.' 
my_car = Car("DeLorean", "silver", 88)
print my_car.display_car()

This worked for me....


#8

Hey you should practice string formatting so your code is more readable and you can save yourself typing more than necessary. For example you could have achieved the same result with your print statement using the code below:

 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 %d MPG." % \
        (self.color, self.model, self.mpg)

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

# Prints - "This is a silver DeLorean with 88 MPG."

#9


somebody helps please? why I still get an error?


#10

It looks like on line 8 you have one long string, so the editor cannot read what %s means since it is in string format.
I'm still learning though, so I could be wrong.


#11

It shows what he asks.
Please I need help. What is wrong ?

class Auto(object):
condicion = "nuevo"
def init(self, modelo, color, kpl):
self.modelo = modelo
self.color = color
self.kpl = kpl

def verAuto(self):
    return "Este es un "+self.modelo+" color "+self.color+" que alcanza "+str(self.kpl)+" kpl."

miAuto = Auto("Clio", "gris", 16)

print miAuto.condicion
print miAuto.modelo
print miAuto.color
print miAuto.kpl

print miAuto.verAuto()

Should show:
"Este es un Clio color gris que alcanza 16 kpl."
My code show that.