Advertisement
horozov86

vehicle_testing_exercise

Jul 25th, 2023
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. from typing import ClassVar
  2.  
  3.  
  4. class Vehicle:
  5.     DEFAULT_FUEL_CONSUMPTION: ClassVar[float] = 1.25
  6.     fuel_consumption: float
  7.     fuel: float
  8.     capacity: float
  9.     horse_power: float
  10.  
  11.     def __init__(self, fuel: float, horse_power: float):
  12.         self.fuel = fuel
  13.         self.capacity = self.fuel
  14.         self.horse_power = horse_power
  15.         self.fuel_consumption = self.DEFAULT_FUEL_CONSUMPTION
  16.  
  17.     def drive(self, kilometers):
  18.         fuel_needed = self.fuel_consumption * kilometers
  19.         if self.fuel < fuel_needed:
  20.             raise Exception("Not enough fuel")
  21.         self.fuel -= fuel_needed
  22.  
  23.     def refuel(self, fuel):
  24.         if self.fuel + fuel > self.capacity:
  25.             raise Exception("Too much fuel")
  26.         self.fuel += fuel
  27.  
  28.     def __str__(self):
  29.         return f"The vehicle has {self.horse_power} " \
  30.                f"horse power with {self.fuel} fuel left and {self.fuel_consumption} fuel consumption"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement