Height in Feet and Inches
The problem: the user types their height in centimetres. Print it in feet and inches — the way it is written on a form or a passport. This is the program where // and % stop being exam trivia and become genuinely useful.
1Plan it first
Work it out on paper before you code it. There are only two facts you need: 1 inch = 2.54 cm and 1 foot = 12 inches. So the trip is cm → inches → feet and inches:
- height in centimetres,
cm
total_inches = cm / 2.54- feet = how many whole 12s fit in it
- inches = what is left over
- the height as x feet y inches
2Splitting a number with // and %
Suppose the height comes to 66.93 inches. How many feet is that? Twelve inches make a foot, so you need two different answers from one division:
How many whole feet fit inside. It throws the leftover away.
What is left over after taking those feet out — the inches.
That is the whole trick, and it works for every “big unit and small unit” problem: rupees and paise, hours and minutes, kilograms and grams.
3The program
# convert a height in centimetres to feet and inches
cm = float(input('Enter your height in centimetres: '))
total_inches = cm / 2.54 # 1 inch = 2.54 cm
feet = int(total_inches // 12) # whole feet: // gives 5.0, int() makes it 5
inches = total_inches % 12 # inches left over
print('Height =', feet, 'feet', round(inches, 1), 'inches')Enter your height in centimetres: 170 Height = 5 feet 6.9 inches
Try your own height:
4The two tidying calls
Two small calls in that program are there only to make the answer readable. Take them out and the maths is identical, but the output is not something you would put on a form:
cm = 170.0
total_inches = cm / 2.54
feet = total_inches // 12
inches = total_inches % 12
print('Height =', feet, 'feet', inches, 'inches')Height = 5.0 feet 6.929133858267718 inches
int(total_inches // 12)It is easy to assume // always gives a whole number — it does not. // gives an int only when both sides are ints. Here total_inches came from cm / 2.54, so it is a float, and 66.93 // 12 is 5.0. Nobody writes '5.0 feet', so int() turns it into 5.
round(inches, 1)6.929133858267718 is true but useless. round(x, 1) keeps one decimal place — 6.9. The rounding happens only in the printing; the variable itself is untouched.
// gives an int only when both sides are ints: 66 // 12 is 5. Give it a float and you get a float back: 66.93 // 12 is 5.0. It has done its job — the decimal part is gone — but the type is still float, which is why the program prints 5.0 feet without the int().Which means there is a second way to write that line, and it is just as correct:
total_inches = 66.929133858267718
feet = int(total_inches // 12) # divide, drop the remainder, then make it an int
feet = int(total_inches / 12) # divide normally, then let int() cut the decimals
print(feet)5
Both give 5, because int() throws away everything after the decimal point — int(5.577) is 5 just as int(5.0) is. Use whichever you find clearer. // says “how many whole 12s” out loud, which pairs nicely with the % on the next line; the / version leans on int() to do the cutting.
int(6.9) is 6, not 7 — it throws the decimal part away. That is exactly what you want for the feet, where the leftover becomes the inches. It is not what you want for the inches themselves, which is why round() is used there instead. Mixing the two up is a logical error: no message, just a height that is slightly wrong.5Check it by hand
Never trust a conversion program until you have checked one case yourself. 170 cm:
170 ÷ 2.54 = 66.93 inches
66.93 ÷ 12 = 5 remainder 6.93
= 5 feet 6.9 inches ✓Which matches the program. Two more worth trying, because they catch different bugs: 152 cm should give 4 feet 11.8 inches (nearly a whole foot left over), and 183 cm should give 6 feet 0.0 inches (almost nothing left over).
6Now you try
- Print the total inches as well, rounded to 2 decimal places, so the user can see the middle step.
- Turn it around: ask for feet and inches, and print the height in cm.
- Use the same
//and%idea to turn a number of seconds into minutes and seconds.
7Recap
cm / 2.54 gives inches), then split it with // for the big unit and % for the leftover. int() tidies the whole part, round(x, 1) tidies the decimal one. The same two operators solve hours and minutes, rupees and paise, and every question like them.A height works out as 66.93 inches. What does 66.93 // 12 give?
Why is int() used for the feet but round() for the inches?