Classes 2.1: Creating Class Methods


#1

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

When I run this code I get:
Traceback (most recent call last):
File "python", line 11, in
TypeError: object.new() takes no parameters

Can someone please help


#2

Just pretend I have the correct indentation


#4

Hi @lukekneip ,

Well, it is possible that your indentation is correct, but we cannot see it. More likely, it is not correct. For example, if the __init__ method is not indented, including its header, it would mean, to the Python interpreter, that it is not part of the Car class. In that case, you would get the message ...

File "python", line 11, in 
TypeError: object.__new__() takes no parameters

#5

This works but displays an error claming not to work

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

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

print my_car.display_car()


#6

You haven't converted 88 to a string, which is why it throws the error. Use str() on self.mpg.


#7

Yes, that was annoying wasn't it. The problem is that you need a period after MPG and it doesn't seem to like some print statements, even with the same exact output.
Here's the error: Oops, try again. It looks like you didn't print "This is a silver DeLorean with 88 MPG.".
Any of these below will print the correct output, but it will only accept the last two as valid:

    print "This is a", self.color,self.model,"with", str(self.mpg),"MPG."
    print "This is a", self.color,self.model,"with", self.mpg,"MPG."
    #Only the below two print statements will provide "Way to go!"
    print "This is a " + self.color + " " + self.model + " with " + str(self.mpg) + " MPG."
    print "This is a %s %s with %s MPG." % (self.color,self.model,self.mpg)

#8

Thanks I realized that


#9

Dude i have some problems with de code in this exercise and solved just moments ago.

Change the variable names because my code is in Spanish.


#10

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

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

a dot(.)missed


#11

or just replace %s to %d next to mpg


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

Now, My output is letter to letter same as the required code.
But, still getting error
Oops, try again. It looks like you didn't print "This is a silver DeLorean with 88 MPG."

Inverted commas, Full stop, capital ans small letters , everything but why am i getting this error now?????


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

This code worked for me.....


#14

'''python

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

#15

man, I wish I had found your reply earlier. Yes, I too missed the full stop but assumed my problems were elsewhere. Thanks !!!