# Chapter 14 Exercise 2 # This program uses a Class named Car and displays the current speed of the car using an accelerate and a break method. # Written by Beth Mela # Last updated June 5, 2015 # Define car class with features: class Car: def __init__(self, yearModel, make, speed): self.yearModel = yearModel self.make = make self.speed = speed # Model accelerate and brake behaviors def accelerate(self,x): self.speed = self.speed + x if(self.speed > 25): self.speed = 25 def brake(self,x): self.speed = self.speed - x if(self.speed < 0): self.speed = 0 # Access Data def get_type(self): print("Car") # Define report status def status(self): print("The speed of the car is currently {} mph.".format(self.speed, end = "")) # Have user enter model year and make yearModel=input("What is the car's model year?\n") make=raw_input("What is the make of the car?\n") speed=0 print('The car being tested is a {} from model year {}.'.format(make,yearModel)) # Speed reports: myCar=Car(yearModel, make, 0) 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