I'm having issues defining a function that controls the correct "speed" (accelerate/slow down).
I currently have my code like this:
class Train():
serial_nummer = 2021001
def __init__(self, type, buildyear):
self.type = type
self.buildyear = buildyear
self.serial_nummer = Train.serial_nummer
self.speed = ()
Train.serial_nummer = 1
return
def snelheid(self, speed):
pass
def __str__(self):
return (f"Train type: {self.type} \nBuildyear: {self.buildyear} \nSerial number: {self.serial_nummer} \nCurrent speed: {self.speed}\n\n")
train1 = Train('Alfatrain', '2001')
train1.speed = 100
print (train1)
How can I create a function that controls the correct "speed"?
Now I'm just modifying the speed with train.speed = 100.
CodePudding user response:
You don't really need a function when you can use train.speed = amount
. You will want to initialize the speed as 0, not an empty tuple, though
Without more clarity, I'm guessing instructions are looking for
def accelerate(self, amount):
self.speed = amount
def decelerate(self, amount):
self.accelerate(-1*amount)