Question 26: If Elif Else

Write a Python program to determine if a car is moving at a safe speed using distance and time.

Instructions:

  1. Ask the user for the car’s name and store it in a variable called car_name.

  2. Ask for the distance the car traveled (in meters) and store it in a variable called distance (as a float).

  3. Ask for the time taken (in seconds) and store it in a variable called time (as a float).

  4. Calculate the speed using the formula:

    speed = distance / time

  5. Use an if-elif-else structure to classify the car’s speed:

    • If speed is greater than 30 m/s, print:
      "Warning, [car_name]! Your speed is [speed] m/s, which is too fast!"
    • If speed is between 20 and 30 m/s (inclusive), print:
      "Caution, [car_name]! Your speed is [speed] m/s. Drive carefully!"
    • If speed is less than 20 m/s, print:
      "Safe, [car_name]! Your speed is [speed] m/s. You are driving safely."
    • If time is 0, print:
      "Error: Time cannot be zero. Please check the input values for [car_name]."
Write what you are looking for, and press enter to begin your search!