Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. class Car:
  2.  
  3. def __init__(self, make, model, year, mpg=25):
  4. self.make = make
  5. self.model = model
  6. self.year = year
  7. self.mpg = mpg
  8.  
  9. self.is_started = False
  10. self.gas = 0
  11.  
  12. def get_car_details(self):
  13. '''returns the car's specifications as a tuple'''
  14. return self.make, self.model, self.year, self.mpg
  15.  
  16. def start_car(self):
  17. '''starts the vehicle'''
  18. self.is_started = True
  19. print('Started car .. ')
  20.  
  21. def add_gas(self, gas):
  22. '''adds gas to car'''
  23. self.gas += gas
  24.  
  25. def drive(self):
  26. '''
  27. line 30 checks if the car is started
  28. and also checks if gas is not 0 (the car has gas)
  29. '''
  30. if not self.is_started:
  31. print('turn on car first')
  32. elif not self.gas:
  33. print('you need to add gas')
  34. else:
  35. print('driving to the store')
  36.  
  37. if self.gas:
  38. self.gas -= 1 # driving consumes gas if the car has gas
  39.  
  40.  
  41. if __name__ == '__main__':
  42. audi = Car('audi', 'a8', 2019)
  43.  
  44. make, model, year, mpg = audi.get_car_details()
  45. print(f'make: {make}\nmodel: {model}\nyear: {year}\nmpg: {mpg}')
  46.  
  47. print('calling drive')
  48. audi.drive()
  49.  
  50. print('starting car')
  51. audi.start_car()
  52.  
  53. print('calling drive')
  54. audi.drive()
  55.  
  56. print('adding 10 gas')
  57. audi.add_gas(10)
  58.  
  59. print('driving')
  60. audi.drive()
  61.  
  62. print('checking gas')
  63. print(audi.gas)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement