Computer Science
Write a Python script that asks the user to enter a length in centimetres. 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 centimetres in an inch.
Python
Python Control Flow
73 Likes
Answer
len = int(input("Enter length in cm: "))
if len < 0:
print("Invalid input")
else:
inch = len / 2.54
print(len, "centimetres is equal to", inch, "inches")Output
Enter length in cm: 150
150 centimetres is equal to 59.05511811023622 inches
Answered By
32 Likes
Related Questions
Which of the following Python programs implement the control flow graph shown?

(a)
while True : n = int(input("Enter an int:")) if n == A1 : continue elif n == A2 : break else : print ("what") else: print ("ever")(b)
while True : n = int(input("Enter an int:")) if n == A1 : continue elif n == A2 : break else : print ("what") print ("ever")(c)
while True : n = int(input("Enter an int:")) if n == A1 : continue elif n == A2 : break print ("what") print ("ever")Find the error. Consider the following program :
a = int(input("Enter a value: ")) while a != 0: count = count + 1 a = int(input("Enter a value: ")) print("You entered", count, "values.")It is supposed to count the number of values entered by the user until the user enters 0 and then display the count (not including the 0). However, when the program is run, it crashes with the following error message after the first input value is read :
Enter a value: 14 Traceback (most recent call last): File "count.py", line 4, in <module> count = count + 1 NameError: name 'count' is not definedWhat change should be made to this program so that it will run correctly ? Write the modification that is needed into the program above, crossing out any code that should be removed and clearly indicating where any new code should be inserted.
A store charges ₹120 per item if you buy less than 10 items. If you buy between 10 and 99 items, the cost is ₹100 per item. If you buy 100 or more items, the cost is ₹70 per item. Write a program that asks the user how many items they are buying and prints the total cost.
Write a program that reads from user — (i) an hour between 1 to 12 and (ii) number of hours ahead. The program should then print the time after those many hours, e.g.,
Enter hour between 1-12 : 9
How many hours ahead : 4
Time at that time would be : 1 O'clock