Featured Posts

CS50 Python , Nutrition Facts Table

Image
Nutrition Facts: Python Practice for Beginners Nutrition Facts Table for Python Practice Welcome to this comprehensive guide for Python beginners! If you are learning how to work with lists, dictionaries, and loops, this post will help you build practical skills using a real-world example: nutrition facts for fruits. Understanding how to organize and manipulate data is a key part of programming, and this exercise will give you hands-on experience. Below is a sample table of fruits and their calorie values, formatted as a Python list of dictionaries. This structure is ideal for coding exercises, projects, or even building your own nutrition calculator. You can expand this list, add new fruits, or use it as a foundation for more advanced Python tasks. Python List of Dictionaries Example: fruits = [ {'name': 'Apple', 'calories': 130}, {'name': 'Avocado', 'calories': 50}, {'name': 'Banana', 'ca...

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

Comments

Popular posts from this blog

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst