Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Car:
- def __init__(self, make, model, year, mpg=25):
- self.make = make
- self.model = model
- self.year = year
- self.mpg = mpg
- self.is_started = False
- self.gas = 0
- def get_car_details(self):
- '''returns the car's specifications as a tuple'''
- return self.make, self.model, self.year, self.mpg
- def start_car(self):
- '''starts the vehicle'''
- self.is_started = True
- print('Started car .. ')
- def add_gas(self, gas):
- '''adds gas to car'''
- self.gas += gas
- def drive(self):
- '''
- line 30 checks if the car is started
- and also checks if gas is not 0 (the car has gas)
- '''
- if not self.is_started:
- print('turn on car first')
- elif not self.gas:
- print('you need to add gas')
- else:
- print('driving to the store')
- if self.gas:
- self.gas -= 1 # driving consumes gas if the car has gas
- if __name__ == '__main__':
- audi = Car('audi', 'a8', 2019)
- make, model, year, mpg = audi.get_car_details()
- print(f'make: {make}\nmodel: {model}\nyear: {year}\nmpg: {mpg}')
- print('calling drive')
- audi.drive()
- print('starting car')
- audi.start_car()
- print('calling drive')
- audi.drive()
- print('adding 10 gas')
- audi.add_gas(10)
- print('driving')
- audi.drive()
- print('checking gas')
- print(audi.gas)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement