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

Erik van Baaren

Let me put this another way. In your code example, you let the definition of car directly follow the class definition. I mean the line car = Car().

If you copy-paste or enter it like that in the REPL, it won’t work. You need to keep hitting enter after the class definition (effectively two times) until you get a new prompt (the >>>).  So if I do this on my pc, it looks like this:

Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 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()
>>> car.increase_speed(10)
You need to start the car first

As mentioned below, I’d suggest staying away from files for now and try to get this working in the REPL first.

Hope that clears it up!