Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Car(object):
- '''
- This is a Car I'm defining
- '''
- def __init__(self):
- '''
- Here we set up some properties
- '''
- self.gasoline = 0
- self.distance = 0
- def run(self, hours):
- '''
- This is a method, so our car can run
- '''
- self.distance += hours * 100 # Our car runs 100 kilometers per hour
- self.gasoline -= hours * 4 # The car takes 4 liters to run 100 kilometers
- def put_gasoline(self, liters):
- '''
- We put gasoline in our car
- '''
- self.gasoline += liters
- def how_is_our_car(self):
- '''
- Let's see how the car goes
- '''
- print "Kilometers run: " + str(self.distance)
- print "Gasoline in tank: " + str(self.gasoline)
- if __name__ == "__main__":
- # Now that we have the class Car, we create two actual
- # car objects. These will behave as cars as we defined
- # with our class
- car_one = Car()
- car_two = Car()
- # We put gas on our cars
- car_one.put_gasoline(40)
- car_two.put_gasoline(50)
- # And then we make them run
- car_one.run(3)
- car_two.run(4)
- # Now we check how each one is doing
- print "Car No. 1"
- car_one.how_is_our_car()
- print "Car No. 2"
- car_two.how_is_our_car()
Add Comment
Please, Sign In to add comment