Thank you for visiting 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 is designed to guide you through key points and clear explanations related to the topic at hand. We aim to make your learning experience smooth, insightful, and informative. Dive in and discover the answers you're looking for!
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
Thank you for reading the article 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 information provided is useful and helps you understand this topic better. Feel free to explore more helpful content on our website!
- You are operating a recreational vessel less than 39 4 feet long on federally controlled waters Which of the following is a legal sound device
- Which step should a food worker complete to prevent cross contact when preparing and serving an allergen free meal A Clean and sanitize all surfaces
- For one month Siera calculated her hometown s average high temperature in degrees Fahrenheit She wants to convert that temperature from degrees Fahrenheit to degrees
Rewritten by : Jeany