High School

We appreciate your visit to Write a program that models two vehicles Car and Truck and simulates driving and refueling them in the summer The Car and Truck both have. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!

Write a program that models two vehicles (Car and Truck) and simulates driving and refueling them in the summer. The Car and Truck both have a fuel quantity and fuel consumption in liters per kilometer. They can be driven given a certain distance and refueled with a specified number of liters.

- In the summer, both vehicles use an air conditioner, which increases their fuel consumption per kilometer:
- The car's consumption increases by 0.9 liters.
- The truck's consumption increases by 1.6 liters.

- The truck has a small hole in its tank, so it receives only 95% of the fuel when refueled.
- The car has no refueling issues and adds all the fuel to its tank.
- If a vehicle cannot travel the given distance, its fuel does not change.

**Input:**
- First line: Information about the car in the format `{Car {fuel quantity} {liters per km}}`
- Second line: Information about the truck in the format `{Truck {fuel quantity} {liters per km}}`
- Third line: Number of commands `N` to be given on the next `N` lines.
- Next `N` lines: 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:
- If successful: `Car/Truck travelled {distance} km`
- If not: `Car/Truck needs refueling`
- Print the distance with two digits after the decimal separator, excluding trailing zeros.
- Finally, print the remaining fuel for both the car and truck:
- `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 :

The Python program that models two vehicles (Car and Truck) and simulates driving and refueling them during the summer is given below.

```python

class Vehicle:

def __init__(self, fuel_quantity, liters_per_km):

self.fuel_quantity = fuel_quantity

self.liters_per_km = liters_per_km

def drive(self, distance, is_summer=False):

if is_summer:

self.fuel_quantity -= distance * (self.liters_per_km + 0.9)

else:

self.fuel_quantity -= distance * self.liters_per_km

return distance if self.fuel_quantity >= 0 else 0

def refuel(self, liters):

self.fuel_quantity += liters

class Car(Vehicle):

def refuel(self, liters):

self.fuel_quantity += liters

class Truck(Vehicle):

def refuel(self, liters):

self.fuel_quantity += 0.95 * liters

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()

action = command[0]

vehicle = command[1]

if action == "Drive":

distance = float(command[2])

if vehicle == "Car":

distance_covered = car.drive(distance, is_summer=True)

if distance_covered > 0:

print(f"Car travelled {distance_covered:.2f} km")

else:

print("Car needs refueling")

elif vehicle == "Truck":

distance_covered = truck.drive(distance, is_summer=True)

if distance_covered > 0:

print(f"Truck travelled {distance_covered:.2f} km")

else:

print("Truck needs refueling")

elif action == "Refuel":

liters = float(command[2])

if vehicle == "Car":

car.refuel(liters)

elif vehicle == "Truck":

truck.refuel(liters)

print(f"Car: {car.fuel_quantity:.2f}")

print(f"Truck: {truck.fuel_quantity:.2f}")

```

This program models the Car and Truck, simulates driving in the summer, and handles refueling. It calculates and prints whether the vehicles can travel a given distance and their remaining fuel quantities as per the specified requirements.

For more such questions on Python program

https://brainly.com/question/27996357

#SPJ4

Thanks for taking the time to read Write a program that models two vehicles Car and Truck and simulates driving and refueling them in the summer The Car and Truck both have. We hope the insights shared have been valuable and enhanced your understanding of the topic. Don�t hesitate to browse our website for more informative and engaging content!

Rewritten by : Barada