1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with Python exercises!

Discussion in 'Scripting & Programming' started by Twina12, Jul 25, 2014.

  1. Twina12

    Twina12 New Member

    2
    0
    1
    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.
    a) Write a function called convert_mileage that converts from
    miles per gallon to liters per 100 kilometers.

    I wrote the program like this:

    Code:
    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. a) Define a function called liters_needed that 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 function convert_mileage that 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 :)
     
  2. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    Litres and Gallons are units of volumetric measurement, they are used for liquids.

    Obviously Miles and Kilometers are ways to measure distance.

    The french invented the metric system of litres and metres.

    The egyptions used to measure in cubits, palms and digits. The units of any measurement system can be quite arbitrary.
    Crash course here.

    You just need to realize that they are different, so a conversion is required. Like Celsius to Fahrenheit.

    When you start do equations in maths, physics, chemistry etc. You realize that the results end up in composite units.

    fuel efficiency = distance traveled / volume fuel used

    So fuel efficiency is mile per gallon (mpg), kilometres per litre

    literally m / g or km / l

    The question is somewhat complicated by the fact there is an imperial and a US gallon, so we'd need to know which one you are using.
    It appears you are using US gallons.

    def litres_needed(distance_in_km, mpg):
    litres_per_100km = convert_mileage(mpg)
    return (distance_in_km/100.0)*litres_per_100km​
     
    Last edited: Jul 26, 2014
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH
  3. Twina12

    Twina12 New Member

    2
    0
    1
    Thanks a lot for your explanation! It helped a lot! However, I had to turn 100 into a float for it to work :) It probably depends on whether or not you are using an older version of Python :)
     
  4. dmarsh

    dmarsh Terabyte Poster

    3,782
    302
    184
    I typed the code into the forum, I didn't test it.

    Yes you need floats for division.
     
    Certifications: CITP, BSc, HND, SCJP, SCJD, SCWCD, SCBCD, SCEA, N+, Sec+, Proj+, Server+, Linux+, MCTS, MCPD, MCSA, MCITP, CCDH

Share This Page

Loading...