Trouble with output :S


#1

Hi,

I'm currently working on the exercise "Creating class methods", Chapter 20.

I have trouble with the output of my programme, which the system doesn't accept, as it seems.

The 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):
        print "This is a", self.color, self.model, "with",str(self.mpg),"MPG."

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

And the error message:

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


#2

Hi,

Note that you have no space for exampe between "This is a" and self.color.


#3

It didn't work :confused:

But thanks for trying to help :slightly_smiling:


#4

Can you post your new code?


#5
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 ", self.color, self.model, "with",str(self.mpg),"MPG."

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

Output:

new
This is a  silver DeLorean with 88 MPG.
None

#6

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


#7

Nevermind, i guess it doesn't work if you devide the strings with commas.


#8

Just like @wizmarco said, it will work.
Tested yours and the program used to compile and execute our codes didn't really accept it so try what Marco said.


#9

That worked, thanks ^^


#10

I tried, but it doesn't accept it that way, it seems :frowning:


#11

Yes, I also was wrong about the missing spaces. If you use print "some string", "another string" a space will be added automatically.


#12

The str type includes a format method that is useful to know. The following produces the correct output, and Codecademy accepts it for the current exercise ...

    def display_car(self):
        return "This is a {:s} {:s} with {:d} MPG.".format(self.color, self.model, self.mpg)

See:
Python » Documentation » The Python Standard Library » str.format(*args, **kwargs)


#13

Wow! Thanks for letting me know that, I really appreciate that :smiley: