So, I'm doing exercises from the Practical Programming book on Python, but I am stuck on the 9th exercise in Chapter 2, which is related to the 7th:
7.: In the United States, a car’s fuel efficiency is measured in miles per gallon. In the metric system, it is usually measured in liters per 100 kilometers. Write a function called
convert_mileagethat converts from miles per gallon to liters per 100 kilometers.
I wrote the program like this:
def convert_mileage(miles_per_gallon):
liters_per_gallon = 3.785411784
kilometers_per_mile = 1.609344
liters_per_100 = (100*liters_per_gallon)/(kilometers_per_mile*miles_per_gallon)
print miles_per_gallon,'miles per gallon are',liters_per_100,'liters per 100 kilometers.'
convert_mileage(40)
convert_mileage(20)
Now, the 9th exercise is the following:
9.: Define a function called
liters_neededthat takes a value representing a distance in kilometers and a value representing gas mileage for a vehicle and returns the amount of gas needed in liters to travel that distance. Your definition should call the functionconvert_mileagethat you defined as part of a previous exercise.
I have no clue how to link the first function into the second one... and I am having difficulties understanding the whole mileage thing compared to liter to travel. If anyone could help me out, that'd be great! Thanks :)
This excercise asks you to reuse your convert_mileage function. Therefore, instead of just printing the calculated value, you have to return it. Change your function to something like this:
LITERS_PER_GALLON = 3.785411784
KILOMETERS_PER_MILES = 1.609344
def convert_mileage(miles_per_gallon):
"""convert miles-per-gallon to liters per 100 kilometers"""
return (100*LITERS_PER_GALLON)/(KILOMETERS_PER_MILES*miles_per_gallon)
Now you can call this function and reuse its result in another calculation:
def liters_needed(distance_km, miles_per_gallon):
"""determine liters needed for distance with given miles per gallon"""
liters_per_100km = convert_mileage(miles_per_gallon)
return liters_per_100km * distance_km / 100
Now you have to print the results when you call the functions:
print "Liters needed for 200km with 15mpg:", liters_needed(200, 15)
#onverting l/100km into mpg
def liters_100km_to_miles_gallon(liters):
kms_per_mile=1.609344
liters_per_gallon=3.785411784
kms_per_liter=100/liters
kms_per_gallon=kms_per_liter*liters_per_gallon
miles_per_gallon=kms_per_gallon/kms_per_mile
return miles_per_gallon
#onverting mpg into 1/100km
def miles_gallon_to_liters_100km(miles):
kms_per_mile=1.609344
liters_per_gallon=3.785411784
gallons_per_100miles=100/miles
gallons_per_100kms=gallons_per_100miles/kms_per_mile
liters_per_100kms=gallons_per_100kms*liters_per_gallon
return liters_per_100kms
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))
# output
60.31143162393162
31.36194444444444
23.52145833333333
3.9007393587617467
7.490910297239916
10.009131205673757
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With