Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Converting Kilometers to Miles | Variables and Types in Python
Introduction to Python

book
Challenge: Converting Kilometers to Miles

Task

Swipe to start coding

The road distance between San Francisco and New York is approximately 4677.4 km. This distance is stored in the variable distance_in_kilometers.

Here's what you need to do:

  • Convert the distance from kilometers (distance_in_kilometers) to miles by dividing it by 1.609. Store the result in the variable distance_in_miles.
  • Create a variable int_distance_in_miles.
  • Convert the variable distance_in_miles to an integer data type.
  • Store the value of distance_in_miles as an integer in the variable int_distance_in_miles.

Solution

# Distance in kilometers
distance_in_kilometers = 4677.4

# Convert to miles
distance_in_miles = distance_in_kilometers / 1.609

# Convert to int
int_distance_in_miles = int(distance_in_miles)

# Testing the `int_distance_in_miles` value
print("San Francisco - New York distance:", int_distance_in_miles, "mi")
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 5
# Distance in kilometers
distance_in_kilometers = 4677.4

# Convert to miles
distance_in_miles = ___ / ___

# Convert to int
int_distance_in_miles = ___

# Testing the `int_distance_in_miles` value
print("San Francisco - New York distance:", int_distance_in_miles, "mi")
toggle bottom row
some-alt