Floating Point, Speed Units - Python Programming Exercise

This Python program prompts the user to input a distance in meters and the time taken in hours, minutes, and seconds, and then calculates the speed in three different units: meters per second, kilometers per hour, and miles per hour. The program uses basic arithmetic and unit conversion to provide the user with a comprehensive understanding of their speed in different units. The conversion from meters to kilometers and from meters to miles is handled by simple multiplication and division based on the known conversion factors (1 kilometer = 1000 meters and 1 mile = 1609 meters). By using this program, users can understand the relationship between distance, time, and speed, while also learning how to convert between different units of measurement. This practical exercise not only reinforces concepts of unit conversion and speed calculation, but also helps to develop skills in working with user input, time handling, and applying mathematical formulas in real-world scenarios.

 Category

Python Data Types

 Exercise

Floating Point, Speed Units

 Objective

Develop a Python program to prompt the user for a distance (in meters) and the time taken (as three numbers: hours, minutes, seconds), and display the speed in meters per second, kilometers per hour, and miles per hour (hint: 1 mile = 1609 meters).

 Example Python Exercise

 Copy Python Code
# Prompt the user for the distance in meters
distance = float(input("Enter the distance in meters: "))

# Prompt the user for the time taken (hours, minutes, seconds)
hours = int(input("Enter the time in hours: "))
minutes = int(input("Enter the time in minutes: "))
seconds = int(input("Enter the time in seconds: "))

# Convert the total time to seconds
total_time_in_seconds = hours * 3600 + minutes * 60 + seconds

# Calculate the speed in meters per second
speed_mps = distance / total_time_in_seconds

# Calculate the speed in kilometers per hour
speed_kph = (distance / 1000) / (total_time_in_seconds / 3600)

# Calculate the speed in miles per hour (1 mile = 1609 meters)
speed_mph = (distance / 1609) / (total_time_in_seconds / 3600)

# Display the results
print(f"Speed: {speed_mps:.2f} meters per second")
print(f"Speed: {speed_kph:.2f} kilometers per hour")
print(f"Speed: {speed_mph:.2f} miles per hour")

 Output

Case 1:
Enter the distance in meters: 5000
Enter the time in hours: 1
Enter the time in minutes: 30
Enter the time in seconds: 0
Speed: 2.78 meters per second
Speed: 10.00 kilometers per hour
Speed: 6.21 miles per hour

Case 2:
Enter the distance in meters: 10000
Enter the time in hours: 2
Enter the time in minutes: 0
Enter the time in seconds: 0
Speed: 1.39 meters per second
Speed: 5.00 kilometers per hour
Speed: 3.11 miles per hour

 Share this Python Exercise

 More Python Programming Exercises of Python Data Types

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Floating Point Sphere

    This Python program calculates both the surface area and volume of a sphere based on its radius. It uses the formulas for surface area (4 * pi * radius squared) and v...

  •  Vowel Check - Conditional

    This Python program prompts the user to input a symbol and categorizes it as a vowel (if it’s a lowercase vowel), a digit, or any other symbol. T...

  •  Northeast Triangle

    This Python program prompts the user to enter a width and then displays a triangle pattern where the number of stars decreases with each row, and underscores (...

  •  Prime Divisors

    This Python program prompts the user to enter a number and then displays that number as a product of its prime factors. The program uses a factorization approa...

  •  Conditional Symbols

    This Python program prompts the user to enter a symbol and determines whether it is an uppercase vowel, a lowercase vowel, a digit, or any other ...

  •  Character Loop

    This Python program uses a "for" loop to print the uppercase letters from "B" to "N". By utilizing Python's range() function and specifying the A...