Advertisement
DiYane

Need for speed III

Sep 18th, 2023
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. class Car:
  2.     def __init__(self, name, mileage, fuel):
  3.         self.name = name
  4.         self.mileage = mileage
  5.         self.fuel = fuel
  6.  
  7.     def drive(self, distance, required_fuel):
  8.         if self.fuel >= required_fuel:
  9.             self.mileage += distance
  10.             self.fuel -= required_fuel
  11.             print(f"{self.name} driven for {distance} kilometers. {required_fuel} liters of fuel consumed.")
  12.         else:
  13.             print("Not enough fuel to make that ride")
  14.  
  15.     def refuel(self, fuel_amount):
  16.         if self.fuel + fuel_amount > 75:
  17.             refueled = 75 - self.fuel
  18.             self.fuel = 75
  19.         else:
  20.             refueled = fuel_amount
  21.             self.fuel += fuel_amount
  22.         print(f"{self.name} refueled with {refueled} liters")
  23.  
  24.     def revert(self, kilometers):
  25.         if self.mileage - kilometers < 10000:
  26.             self.mileage = 10000
  27.         else:
  28.             self.mileage -= kilometers
  29.             print(f"{self.name} mileage decreased by {kilometers} kilometers")
  30.  
  31.     def __str__(self):
  32.         return f"{self.name} -> Mileage: {self.mileage} kms, Fuel in the tank: {self.fuel} lt."
  33.  
  34.  
  35. n = int(input())
  36. cars = {}
  37.  
  38. for _ in range(n):
  39.     car_info = input().split("|")
  40.     car_name, mileage, fuel = car_info[0], int(car_info[1]), int(car_info[2])
  41.     cars[car_name] = Car(car_name, mileage, fuel)
  42.  
  43. while True:
  44.     command = input()
  45.     if command == "Stop":
  46.         break
  47.  
  48.     command_parts = command.split(" : ")
  49.     action, car_name = command_parts[0], command_parts[1]
  50.  
  51.     if action == "Drive":
  52.         distance, fuel = int(command_parts[2]), int(command_parts[3])
  53.         cars[car_name].drive(distance, fuel)
  54.         if cars[car_name].mileage >= 100000:
  55.             print(f"Time to sell the {car_name}!")
  56.             del cars[car_name]
  57.     elif action == "Refuel":
  58.         fuel_amount = int(command_parts[2])
  59.         cars[car_name].refuel(fuel_amount)
  60.     elif action == "Revert":
  61.         kilometers = int(command_parts[2])
  62.         cars[car_name].revert(kilometers)
  63.  
  64. for car in cars.values():
  65.     print(car)
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement