7/11 error print (ES) SOLVED


#1

My code is OK, but it show me an error.

This is my code:

 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 modelo %s color %s que alcanza %s kpl." % (self.modelo, self.color, self.kpl)

 miAuto = Auto("Clio", "gris", 16)
 print miAuto.condicion
 print miAuto.modelo
 print miAuto.color
 print miAuto.kpl
 print miAuto.verAuto()

and this is the error:

¡Uy! Probá otra vez.
    No hiciste que se mostrara la descripción correcta, deberia ser 
'Este es un Clio color gris que alcanza 16 kpl.', utiliza + para 
concatenar strings y str para kpl

#2

Today, I tried again and it worked.


#3

Check all your code and after compare to this:

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, str(self.mpg))

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

print my_car.display_car()

Ps. Don´t forget tabs...


#4

Editor note me that "Oops, try again. It looks like you didn't print "This is a silver DeLorean with 88 MPG.".

My code is below. Please help me, thankss

class Car(object):
condition = "new"
car=''
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,str(self.mpg))

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


#5

you forgot a dot in the message after MPG
return 'This is a %s %s with %s MPG.' %(self.color,self.model,str(self.mpg))


#6

Having the same problem. It says I didn't print the statement when that's exactly what I printed

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

Is is being finicky about the way I'm choosing to print? Is this a bug?


#7

God, Thank you Bogdyy13.


#8

I think you should return the string instead of printing it in display_car() method


#9

"This is a %s %s with a %s MPG." %(self.color, self.model, str(self.mpg))
should be:
"This is a %s %s with %s MPG." %(self.color, self.model, str(self.mpg))
You put "a". :smile:


#10

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, str(self.mpg))

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

I'm getting Oops, try again. Make sure your Car class has a display_car() method. and the console says AttributeError: 'Car' object has no attribute 'display_car'


#11

it work only if you change to this
def init(self, model, color, mpg):


#12

Not to be picky, but shouldn't it be like:

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

with the underscores? I actually had one fail on me a few lessons ago because I didn't realize that it was two underscores on each side of "init".

Also, about concatenating that string:

class Car(object):
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg = mpg
    def display_car(self):
        return str("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()

Something else I noticed is you guys were actually doing another string conversion on the decimal value 88, instead of using that %d operator instead of %s. So like, in the body of the string, you were calling another string data type to use with the % operator, then converting the last variable self.mpg to a string right before passing it to that third "%s" in the string body. Which it's like, whatever, that works too, but it seems weird to me to do it that way, to do that sort of double flip/double str() conversion when you don't have to.

Have we just not been told at this point in the course about all those string % operator choices? I grabbed %d from a reference website somewhere else, but I thought for sure they went over all those back when we were doing a bunch of string stuff? I can't remember.


#13

I did this, similar to all the codes here, but return string is different:

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.condition
print my_car.model
print my_car.color
print my_car.mpg

print my_car.display_car()