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.

5 Answers5

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

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    better to use `string.format()` ? and its should be `%d` for `mpg`? – Grijesh Chauhan Aug 20 '13 at 19:15
  • @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 –  Aug 20 '13 at 19:16
  • @GrijeshChauhan yup, I've added options using `format`. `%d` might be more correct here, agreed. Thanks. – alecxe Aug 20 '13 at 19:18
  • @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. – alecxe Aug 20 '13 at 19:19
  • I am reader on Python tag (rarely answers as new learner). So I have a question `%s` will behave correctly with `mpg` (auto typecasting happens?) – Grijesh Chauhan Aug 20 '13 at 19:20
  • 1
    Well, take a look http://stackoverflow.com/questions/15170349/whats-the-difference-between-r-s-and-d-in-python and http://stackoverflow.com/questions/4288973/s-and-d-python. – alecxe Aug 20 '13 at 19:22
  • @alecxe Ok Boss I'll take care of It . Thanks Once again. –  Aug 20 '13 at 19:24
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()
Óscar López
  • 232,561
  • 37
  • 312
  • 386
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.

Benjamin Peterson
  • 19,297
  • 6
  • 32
  • 39
1

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

Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88
0

The line

print my_car.display_car()

should be

my_car.display_car()
Edward
  • 3,292
  • 1
  • 27
  • 38
misalabs
  • 11
  • 1