#Week 10 Ex: 2 #This object oriented program creates a car. #When it runs it reports the status of the car accelerating 5 times, #Then it reports the status of slowing from 25 MPH to 0 MPH 5 times. #Written by Greg Lamb #Last modified 05/28/2015 #Create an object class class Car: #Give the car/object some attributes def __init__(self, yearModel, carMake, speed): self.__yearModel = yearModel self.__carMake = carMake 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 def setYearModel(self, yearModel): self.__yearModel = yearModel if yearModel <= 1920 or yearModel >= 2015: print("You must enter a four digit year between 1921 - 2014") else: yearModel = 1960 print("Default Model Year is 1960") def setCarMake(self, carMake): self.__carMake = carMake print("Please enter the name of an auto maker") def setSpeed(self, speed): self.__speed = speed def getYearModel(self): return self.__yearModel def getCarMake(self): return self.__carMake def getSpeed(self): return self.__speed #Access Data def get_type(self): print("Car") #Report status def status(self): print("The speed of the {} {} is currently {} MPH".format(self.__yearModel,self.__carMake,self.__speed, end = "")) #User enters their car data print("The car's test will begin at 0 MPH, it will then accelerate in 5 MPH increments until reaching 25 MPH.\nUpon reaching 25 MPH, it will begin braking in 5 MPH increments until reaching 0 MPH.\n") yearModel=input("What is the model year of the car being tested?\n") carMake=raw_input("What is the name of the car's manufacturer?\n") speed = 0 print("The car being tested is a {} made by {} currently traveling at {}.\n".format(yearModel, carMake, speed)) #Call the incremental speed reports myCar=Car(yearModel, carMake, 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