Classes


#1

class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def drive_car(self, condition):
self.condition = "used"
return self.condition
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)
drive_car()
print car.condition

`
The interpreter says the method is not defined and the browser asks if i passed self to it.
Please be didatic :slight_smile:


#2

what is car?
methods can be used on objects you create, but you haven't created an object named "car".
however, you have created my_car


#3

thanks I thougt so but this also doesnt work:

class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def drive_car(self, condition):
self.condition = "used"
return self.condition
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
drive_car(my_car)
print my_car.condition


#4

This is also the wrong syntax.
Should be my_car.drive_car()
when you're calling a method with an object, you'll use dot notation (that's what is taught here)


#5

Thanks you are being great.
class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def drive_car(self, condition):
self.condition = "used"
return self.condition
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
my_car.drive_car(my_car)
print my_car.condition

is this correct? if not why? the windows on the right gives the expected output while the browser thing complains about self not being passed to the drive_car method can you please explain me why?


#6

Try putting

```python

above the code
and

```

below the code


#7

you mean in the discussion topic? ok.

' ' '
class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def drive_car(self, condition):
self.condition = "used"
return self.condition
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
my_car.drive_car(my_car)
print my_car.condition

' ' '


#8
' ' '

are not

```

Also, adding the language names at the first one helps get the right colors sometimes.

You can also indent everything by 4 spaces, or highlight it and click the appropriate button in the tools in the post editor

Cause, you know, can't run that. At the very least the reader would have to put in considerable extra effort to compensate for missing information in the code


#9

python

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def drive_car(self, condition):
        self.condition = "used" 
        return self.condition
    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
my_car.drive_car(my_car)
print my_car.condition

#10

Solved.
'''
python

class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def drive_car(self):
self.condition = "used"
return self.condition
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
my_car.drive_car()
print my_car.condition
'''


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.