Reply To: Classes and Objects in Python

Welcome to Python Land Forums The Python Tutorial Classes and Objects in Python Reply To: Classes and Objects in Python

#1893
guillaume
Participant

Hi Erik,

Getting the error again

>>> car.increase_speed(10)
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
NameError: name ‘car’ is not defined

class Car:
    speed = 0
    started = False
 
    def start(self):
        self.started = True
        print("Car started, let's ride!")
 
    def increase_speed(self, delta):
        if self.started:
            self.speed = self.speed + delta
            print('Vrooooom!')
        else:
            print("You need to start the car first")
 
    def stop(self):
        self.speed = 0
        print('Halting')
car = Car()