Advertisement

Responsive Advertisement

Write a program that asks the user to enter a length in centimeters. If the user enters a negative length, the program should tell the user that the entry is invalid. Otherwise, the program should convert the length to inches and print out the result. There are 2.54 centimeters in an inch.

 length_cm = input("Enter a length in centimeters: ")


# Check if the input is a valid positive number

if length_cm.isdigit() and int(length_cm) > 0:

  # Convert the length to inches

  length_inches = int(length_cm) / 2.54


  # Print the result

  print(f"{length_cm} centimeters is equal to {length_inches:.2f} inches.")

else:

  print("Invalid entry. Please enter a positive number.")

The output of the program would depend on what the user enters as input. Here are some example outputs:

Input: 10
Output: 10 centimeters is equal to 3.94 inches.

Input: -5
Output: Invalid entry. Please enter a positive number.

Input: abc
Output: Invalid entry. Please enter a positive number.

Post a Comment

0 Comments