Guest User

Untitled

a guest
Jul 19th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. class Car(object):
  2.    '''
  3.   This is a Car I'm defining
  4.   '''
  5.    def __init__(self):
  6.        '''
  7.       Here we set up some properties
  8.       '''
  9.        self.gasoline = 0
  10.        self.distance = 0
  11.  
  12.    def run(self, hours):
  13.        '''
  14.       This is a method, so our car can run
  15.       '''
  16.        self.distance += hours * 100       # Our car runs 100 kilometers per hour
  17.        self.gasoline -= hours * 4         # The car takes 4 liters to run 100 kilometers
  18.  
  19.    def put_gasoline(self, liters):
  20.        '''
  21.       We put gasoline in our car
  22.       '''
  23.        self.gasoline += liters
  24.  
  25.    def how_is_our_car(self):
  26.        '''
  27.       Let's see how the car goes
  28.       '''
  29.        print "Kilometers run: " + str(self.distance)
  30.        print "Gasoline in tank: " + str(self.gasoline)
  31.  
  32.  
  33. if __name__ == "__main__":
  34.      # Now that we have the class Car, we create two actual
  35.      # car objects. These will behave as cars as we defined
  36.      # with our class
  37.      car_one = Car()
  38.      car_two = Car()
  39.  
  40.      # We put gas on our cars
  41.      car_one.put_gasoline(40)
  42.      car_two.put_gasoline(50)
  43.  
  44.      # And then we make them run
  45.      car_one.run(3)
  46.      car_two.run(4)
  47.  
  48.      # Now we check how each one is doing
  49.      print "Car No. 1"
  50.      car_one.how_is_our_car()
  51.      print "Car No. 2"
  52.      car_two.how_is_our_car()
Add Comment
Please, Sign In to add comment