2

I'm doing python programming and this is class unit of that. and the code is not printing the right answer.

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

I'm trying to print This is a silver DeLorean with 88 MPG.

2
  • 2
    But what it does print? Commented Aug 20, 2013 at 19:09
  • Kaushik, also add spaces: '...self.color + " " +self.model...' Commented Aug 20, 2013 at 19:13

5 Answers 5

5

Try this version of display_car method instead:

def display_car(self):
    print "This is a %s %s with %d MPG." % (self.color, self.model, self.mpg)

Or, you can make use of format:

def display_car(self):
    print "This is a {0} {1} with {2} MPG.".format(self.color, self.model, self.mpg)

Both versions print This is a silver DeLorean with 88 MPG.

I think you see that both versions are more readable than your's with string concatenation.

You can make it even more readable by using format with named arguments:

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

Also, you have None printed too - replace print my_car.display_car() with my_car.display_car().

Sign up to request clarification or add additional context in comments.

7 Comments

better to use string.format() ? and its should be %d for mpg?
@alecxe I would like to know my concatenation statement is right or not ? if i wanna print This is a silver DeLorean with 88 MPG with spaces in them only one after one word
@GrijeshChauhan yup, I've added options using format. %d might be more correct here, agreed. Thanks.
@Kaushik your concatenation statement is almost right except that you are missing whitespaces. It's better to use options I've mentioned - less error prompt and more readable.
|
4

Change this:

def display_car(self):
    return "This is a "+ self.color + self.model+ " with "+str(self.mpg)+"MPG"

You see, the display_car method must return the value to print. Alternatively, you could leave display_car() as it is, but instead call the method like this:

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

Comments

1

The print in print my_car.display_car() is redundant because you've already printed the statement in the display_car method. Thus, you get an extra None.

2 Comments

so what should i do ? the display_car method print statement should be replaced by return?
That's one option. Alternatively, just write my_car.display_car() (without of the print in front).
1

Python implicitly returns None if you don't return anything so printing a function that calls print will also print None.

Comments

0

The line

print my_car.display_car()

should be

my_car.display_car()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.