Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Operators and Expressions

Calculate speed = distance/time and convert units - Python Program

Example 1 :

distance_km = float(input("Enter distance (km): ")) time_hr = float(input("Enter time (hr): ")) speed_kmph = distance_km / time_hr speed_mps = (distance_km * 1000) / (time_hr * 3600) print(f"Speed = {speed_kmph:.2f} km/h") print(f"Speed = {speed_mps:.2f} m/s")

Output

 
OUTPUT  :
Enter distance (km): 120
Enter time (hr): 2
Speed = 60.00 km/h
Speed = 16.67 m/s
 

Example 2 :

def calculate_speed(distance, time, distance_unit, time_unit): """ Calculates speed and allows for unit conversions. Args: distance (float): The distance traveled. time (float): The time taken. distance_unit (str): The unit of distance (e.g., 'km', 'miles', 'm'). time_unit (str): The unit of time (e.g., 'hours', 'minutes', 'seconds'). Returns: tuple: A tuple containing the calculated speed and its unit (e.g., (50.0, 'km/h')). """ # Convert distance to a common base unit (meters for consistency) if distance_unit.lower() == 'km': distance_meters = distance * 1000 elif distance_unit.lower() == 'miles': distance_meters = distance * 1609.34 elif distance_unit.lower() == 'm': distance_meters = distance else: return None, "Unsupported distance unit." # Convert time to a common base unit (seconds for consistency) if time_unit.lower() == 'hours': time_seconds = time * 3600 elif time_unit.lower() == 'minutes': time_seconds = time * 60 elif time_unit.lower() == 'seconds': time_seconds = time else: return None, "Unsupported time unit." if time_seconds == 0: return None, "Time cannot be zero." speed_mps = distance_meters / time_seconds # Speed in meters per second # Convert speed to desired output unit (e.g., km/h, mph) output_speed_unit = f"{distance_unit.lower()}/{time_unit.lower()}" if distance_unit.lower() == 'km' and time_unit.lower() == 'hours': speed_output = speed_mps * 3.6 # Convert m/s to km/h elif distance_unit.lower() == 'miles' and time_unit.lower() == 'hours': speed_output = speed_mps * 2.23694 # Convert m/s to mph else: speed_output = speed_mps # Default to m/s if no specific conversion defined return speed_output, output_speed_unit # User input try: distance_input = float(input("Enter the distance: ")) distance_unit_input = input("Enter the distance unit (km, miles, m): ") time_input = float(input("Enter the time: ")) time_unit_input = input("Enter the time unit (hours, minutes, seconds): ") speed, unit = calculate_speed(distance_input, time_input, distance_unit_input, time_unit_input) if speed is not None: print(f"\nThe calculated speed is: {speed:.2f} {unit}") else: print(f"\nError: {unit}") except ValueError: print("\nInvalid input. Please enter numeric values for distance and time.")

Output

 
OUTPUT 1 :
Enter the distance: 120
Enter the distance unit (km, miles, m): km
Enter the time: 245
Enter the time unit (hours, minutes, seconds): hours

The calculated speed is: 0.49 km/hours

OUTPUT 2 :
Enter the distance: 120
Enter the distance unit (km, miles, m): m
Enter the time: 60
Enter the time unit (hours, minutes, seconds): seconds

The calculated speed is: 2.00 m/seconds