0
Write a problem that asks the user to enter a whole number of inches and converts that length to feet and inches.
Can you please explain the code too. Thank you.
2 Respostas
+ 5
total_inches = int(input("input length in inches: "))
feet = total_inches // 12
inches_remained = total_inches % 12
print("{0} inches equals {1} feet and {2} inches".format(total_inches, feet, inches_remained))
-----
As we know 1 feet equals 12 inches so any number of inches can be presented in feet and inches according to formula:
total_inches = 12*feet + inches_remained, where inches_remained is integer from 0 to 11.
1st line: getting the 'total_inches' from user and converting it from string to integer
2nd line: calculating 'feet' using floor division
3rd line: performing modulo operation to get 'inches_remained'
4th line: printing the results using format method.
0
thank you