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!
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!
- Why do Businesses Exist Why does Starbucks Exist What Service does Starbucks Provide Really what is their product.
- The pattern of numbers below is an arithmetic sequence tex 14 24 34 44 54 ldots tex Which statement describes the recursive function used to..
- Morgan felt the need to streamline Edison Electric What changes did Morgan make.
Rewritten by : Barada