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
Show 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