Compute Car’s Fuel Consumption
FUEL CONSUMPTION PROGRAM
Coding Project in Python
In this coding project in Python you must calculate the gasoline consumption of a car based on the kilometers traveled, average speed, and number of passengers in the car during a 14-day period.
The last two weeks, you have written down the following data about the car and how you used it:
Kilometers traveled each day:
Monday 1: 10
Tuesday 2: 12
Wednesday 3: 8
Thrusday 4: 8
Friday 5: 16
Saturday 6: 380
Sunday 7: 9
Monday 8: 7
Tuesday 9: 9
Wednesday 10: 9
Thrusday 11: 8
Friday 11: 20
Saturday 12: 47
Sunday 13: 0
Average speed each day (km/h):
Monday 1: 50
Tuesday 2: 60
Wednesday 3: 80
Thrusday 4: 50
Friday 5: 60
Saturday 6: 110
Sunday 7: 75
Monday 8: 65
Tuesday 9: 75
Wednesday 10: 60
Thrusday 11: 50
Friday 11: 80
Saturday 12: 100
Sunday 13: 0
Passengers in the car ech day
Monday 1: 1
Tuesday 2: 1
Wednesday 3: 1
Thrusday 4: 1
Friday 5: 2
Saturday 6: 4
Sunday 7: 2
Monday 8: 1
Tuesday 9: 1
Wednesday 10: 1
Thrusday 11: 1
Friday 11: 1
Saturday 12: 2
Sunday 13: 0
Assume that the car uses 5 liters of gasoline per 100 km with 1 passenger. For each additional passenger, add 0.5 liters.
Write a Python program that calculates and displays the gasoline consumption in liters for each day and the total consumption over the two-week period.
It is Python practice project for intermediate students. Please make sure to adhere to the given format and instructions while maintaining the original language used.
Hints
- Define dictionaries to store the kilometers traveled, average speed, and passengers in the car each day.
- Define the car’s fuel consumption parameters.
- Calculate the gasoline consumption for each day and keeps track of the total consumption.
- Use a loop to iterate through the days and calculate the fuel consumption using the given formula.
- Finally, print the gasoline consumption for each day and the total gasoline consumption over the 14 days.
Expected ouput
Day 1: Gasoline consumption - 0.50 liters Day 2: Gasoline consumption - 0.60 liters Day 3: Gasoline consumption - 0.40 liters Day 4: Gasoline consumption - 0.40 liters Day 5: Gasoline consumption - 1.30 liters Day 6: Gasoline consumption - 20.50 liters Day 7: Gasoline consumption - 0.95 liters Day 8: Gasoline consumption - 0.35 liters Day 9: Gasoline consumption - 0.45 liters Day 10: Gasoline consumption - 0.45 liters Day 11: Gasoline consumption - 0.40 liters Day 12: Gasoline consumption - 2.35 liters Day 13: Gasoline consumption - -0.50 liters Total gasoline consumption over 14 days: 28.15 liters
Source Code
# Define the data in dictionaries
km = {'1': 10, '2': 12, '3': 8, '4': 8, '5': 16, '6': 380, '7': 9, '8': 7, '9': 9, '10': 9, '11': 8, '12': 47, '13': 0}
avg_speed = {'1': 50, '2': 60, '3': 80, '4': 50, '5': 60, '6': 110, '7': 75, '8': 65, '9': 75, '10': 60, '11': 50, '12': 80, '13': 100}
passengers = {'1': 1, '2': 1, '3': 1, '4': 1, '5': 2, '6': 4, '7': 2, '8': 1, '9': 1, '10': 1, '11': 1, '12': 1, '13': 0}
# Define the car's fuel consumption
base_consumption = 5 # liters per 100 km
additional_consumption_per_passenger = 0.5 # liters
# Calculate the gasoline consumption each day
total_gas_consumption = 0
for day in km:
fuel_consumption = (km[day] / 100) * base_consumption + (passengers[day] - 1) * additional_consumption_per_passenger
total_gas_consumption += fuel_consumption
print(f"Day {day}: Gasoline consumption - {fuel_consumption:.2f} liters")
# Print the total gasoline consumption over the 14 days
print(f"Total gasoline consumption over 14 days: {total_gas_consumption:.2f} liters")
