class Car: # attributes __yearModel = "" __carMake = "No brand yet" __speed = 0 # methods def setCarMake(self,): self.brand = x def setMaxSpeed(self,x): self.maxSpeed = x def printData(self): print("The car was manufactured by", self.brand, ",") print("and its maximum speed is", self.maxSpeed, "km/h.") class Car: def __init__(self, speed): self.speed = speed print("You have just created a vehicle.") def accelerate(self,x): self.speed = self.speed + x def brake(self,x): self.speed = self.speed - x def status(self): print("The speed of the vehicle is".format(self.speed, end=" km/h.")) class Car: def __init__(self, speed, maxSpeed): self.speed = speed self.maxSpeed = maxSpeed #print("You have just created a vehicle.") def accelerate(self,x): self.speed = self.speed + x if(self.speed > self.maxSpeed): self.speed = self.maxSpeed def brake(self,x): self.speed = self.speed - x if(self.speed < 0): self.speed = 0 def status(self): print("The speed of the Model T is {}".format(self.speed, end=" mph.")) myCar = Car(0,25) myCar.status() myCar.accelerate(5) myCar.status() myCar.accelerate(5) myCar.status() myCar.accelerate(5) myCar.status() myCar.accelerate(5) myCar.status() myCar.accelerate(5) myCar.status() myCar.brake(5) myCar.status() myCar.brake(5) myCar.status() myCar.brake(5) myCar.status() myCar.brake(5) myCar.status() myCar.brake(5) myCar.status()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question