-->

Python 4.3.1.10 LAB: Converting fuel consumption

 Python 4.3.1.10 LAB: Converting fuel consumption

4.3.1.10 LAB: Converting fuel consumption

4.3.1.10 LAB

Objectives

  • improving the student's skills in defining, using and testing functions.

Scenario

A car's fuel consumption may be expressed in many different ways. For example, in Europe, it is shown as the amount of fuel consumed per 100 kilometers.

In the USA, it is shown as the number of miles traveled by a car using one gallon of fuel.

Your task is to write a pair of functions converting l/100km into mpg "mile per gallon", and vice versa.

Sandbox
The functions:

are named liters_100km_to_miles_gallon and miles_gallon_to_liters_100km respectively;

take one argument (the value corresponding to their names)

Complete the code in the editor.

Run your code and check whether your output is the same as ours.

Here is some information to help you:

1 American mile = 1609.344 metres; 1 mile = "1.60934" meters

1 American gallon = 3.785411784 litres.

Expected output

  • 60.31143162393162
  • 31.36194444444444
  • 23.52145833333333
  • 3.9007393587617467
  • 7.490910297239916
  • 10.009131205673757

Solution Code:

def liters_100km_to_miles_gallon(liters):
    gallons = liters / 3.785411784
    # 1 mile = "1.60934" meters
    # 1 km   = 1000 meters .. 1000 * 1.60934 = 1609.344
    # 100km = 100 * 1000 meters (The function works on 100 km)
    miles = 100 * 1000 / 1609.344
    return miles / gallons

def miles_gallon_to_liters_100km(miles):
    km_100 = miles * 1609.344 * 1000 / 100
    liters = 3.785411784
    return liters / km_100

print(liters_100km_to_miles_gallon(3.9))
print(liters_100km_to_miles_gallon(7.5))
print(liters_100km_to_miles_gallon(10.))
print(miles_gallon_to_liters_100km(60.3))
print(miles_gallon_to_liters_100km(31.4))
print(miles_gallon_to_liters_100km(23.5))

=================================================================================

Follow the Python page on the Blog to be the first to know

Post a Comment

0 Comments