In: Computer Science
Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of fuel by recording the kilometres driven and the litres used for each tankful. Develop a python program that prompts the user to input the kilometres driven and litres used for each tankful. The program should calculate and display the kilometres per litre obtained for each tankful. Read inputs for at least 10 such readings. After processing all input information, the program should calculate and print the combined kilometres per litre obtained for all tankfuls (= total kilometres driven divided by total litres used).
It must be in Python
Python code:
#accepting kilometres driven
km=float(input("Enter kilometres driven(-1 to quit): "))
#initializing total_km as 0
total_km=0
#initializing total_litre as 0
total_litre=0
#looping till km entered is -1
while(km!=-1):
#accepting litres used
litre=float(input("Enter litres used: "))
#printing Kilometres per litre
print("Kilometres per litre:",km/litre)
#adding km to total_km
total_km+=km
#adding litre to total_litre
total_litre+=litre
#accepting kilometres driven
km=float(input("Enter kilometres driven(-1 to quit): "))
#printing Total kilometres per litre
print("Total kilometres per litre:",total_km/total_litre)
Screenshot:
Input and Output: