6/11 and 7/11


#1

6/11 - this should refer to instance variables rather than member variables?

7/11 - I am printing the correct string - but I am getting a Codecademy error saying that I am not printing the correct string. here is my code:

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.condition

print my_car.display_car()

Does anyone have any ideas?


#2

I figured out the error on 7/11 - I was not printing out the full stop at the end of the string!


#3

My code looks exactly like yours, and I'm getting the error message:
"Oops, try again. Make sure you pass the self keyword to the display_car() method."
I don't understand what's wrong. What do you mean by not printing out the full stop at the end of the string?


#4

I got it right, I don't know what was wrong, but i rewrote everything from the beginning and it was correct.


#5

Hey arcwhiz,

Congrats on getting it right!

What I meant was the difference between these two strings:

'This is a %s %s with %s MPG'

'This is a %s %s with %s MPG.'

^The latter is correct, the former is incorrect.


#6

Seems like both versions of this line should not work but they do.

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

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

It sounds like the first version with str(self.mpg) is what the instructions ask for to convert 88 to the string "88". Does anyone else have input on whether both should work?


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

The above is what worked for me and gave this output:

This is a silver DeLorean with 88 MPG.
None

However, when I didn't use the string function on (self.mpg), the output was this:

    This is a 'silver' 'DeLorean' with 88 MPG.
    None

The above wasn't accepted as a correct answer. For some reason now, when I go back to try both ways, they both work. It wasn't until I added the string function was I able to get it to work and move on.


#8

I actually did it without the str() first and it worked. Then went back and tried it with the str(). I have no idea at this point.


#9

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.condition
print my_car.display_car()

For the code above, I received an answer:

File "python", line 11
my_car = Car("DeLorean", "silver", 88)

SyntaxError: invalid syntax
Oops, try again. Did you accidentally delete my_car?

Do you know why?


i've finally found it: there was one ) missing after:
return "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg)

so it should look like:
return "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg))

and is OK:)


#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()


#11

Hello,
do you have any solution for that error?? I am getting the exact same error.


#12

can we get your code ?


#13

hi @boardsurfer92218 @manoj_kumar here check my code and compare it with yours maybe you will find what is the mistake :smile:

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

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

my_car.display_car()

just take care of the indentation levels in your editor.