Problem 1: Vehicles

Write a program that models two vehicles (Car and Truck) and can simulate driving and refueling them in the summer.

- Both the Car and Truck have fuel quantity and fuel consumption in liters per km. They can be driven a given distance and refueled with a given number of liters.
- In the summer, both vehicles use air conditioning, increasing their fuel consumption per km by 0.9 liters for the car and 1.6 liters for the truck.
- The truck has a small leak in its tank, receiving only 95% of the refueled amount. The car adds all given fuel to its tank with no issues.
- If a vehicle cannot travel the given distance, its fuel does not change.

**Input**

1. On the first line, provide information about the car in the format: `Car {fuel quantity} {liters per km}`
2. On the second line, provide information about the truck in the format: `Truck {fuel quantity} {liters per km}`
3. On the third line, input the number of commands, N, that will be provided on the following N lines.
4. On the next N lines, provide commands in the format:
- `Drive Car {distance}`
- `Drive Truck {distance}`
- `Refuel Car {liters}`
- `Refuel Truck {liters}`

**Output**

After each "Drive" command, print whether the Car/Truck was able to travel the given distance in the following format:

- If successful: `Car/Truck travelled {distance} km`
- If not: `Car/Truck needs refueling`

Print the remaining fuel for both the car and truck at the end:

- `Car: {liters}`
- `Truck: {liters}`

**Example #1**

**Input**
```
Car 15 0.3
Truck 100 0.9
4
Drive Car 9
Drive Car 30
Refuel Car 50
Drive Truck 10
```

**Output**
```
Car travelled 9 km
Car needs refueling
Truck travelled 10 km
Car: 54.20
Truck: 75.00
```

**Example #2**

**Input**
```
Car 30.4 0.4
Truck 99.34 0.9
5
Drive Car 500
Drive Car 13.5
Refuel Truck 10.300
Drive Truck 56.2
Refuel Car 100.2
```

**Output**
```
Car needs refueling
Car travelled 13.5 km
Truck needs refueling
Car: 113.05
Truck: 109.13
```

Answer :

A program that models 2 vehicles (Car and Truck) and will be able to simulate driving and refueling them in the summer is given.

How to depict the program

class Car:

def __init__(self, fuel_quantity, liters_per_km):

self.fuel_quantity = fuel_quantity

self.liters_per_km = liters_per_km

def drive(self, distance):

fuel_required = distance * (self.liters_per_km + 0.9) # increased consumption with AC

if fuel_required <= self.fuel_quantity:

self.fuel_quantity -= fuel_required

return f"Car travelled {distance:.2f} km"

else:

return "Car needs refueling"

def refuel(self, liters):

self.fuel_quantity += liters

def __str__(self):

return f"Car: {self.fuel_quantity:.2f}"

class Truck:

def __init__(self, fuel_quantity, liters_per_km):

self.fuel_quantity = fuel_quantity

self.liters_per_km = liters_per_km

def drive(self, distance):

fuel_required = distance * (self.liters_per_km + 1.6) # increased consumption with AC

if fuel_required <= self.fuel_quantity:

self.fuel_quantity -= fuel_required

return f"Truck travelled {distance:.2f} km"

else:

return "Truck needs refueling"

def refuel(self, liters):

self.fuel_quantity += liters * 0.95 # 95% of the given fuel due to the hole in the tank

def __str__(self):

return f"Truck: {self.fuel_quantity:.2f}"

car_info = input().split()

car = Car(float(car_info[1]), float(car_info[2]))

truck_info = input().split()

truck = Truck(float(truck_info[1]), float(truck_info[2]))

n = int(input())

for _ in range(n):

command = input().split()

if command[0] == "Drive":

if command[1] == "Car":

result = car.drive(float(command[2]))

print(result)

elif command[1] == "Truck":

result = truck.drive(float(command[2]))

print(result)

elif command[0] == "Refuel":

if command[1] == "Car":

car.refuel(float(command[2]))

elif command[1] == "Truck":

truck.refuel(float(command[2]))

print(car)

print(truck)

Learn more about program on

https://brainly.com/question/26642771

#SPJ1

Other Questions