156 Chapter 7
At u, we enter the two conversion formulas we developed.
Both functions take an input parameter (inches and pounds), and
each function returns a value. At v, we ask the user for a height
and weight and store those values in height_in and weight_lb.
At w, we call the convert_in2cm() function, passing height_in as
the value we want to convert, and we store the converted answer
in the variable height_cm. We perform another conversion calcula-
tion at x using the convert_lb2kg() function to convert the person’s
weight in pounds (abbreviated as lbs) into the equivalent in kilo-
grams (kg).
The equation at y does two things: first, it divides the user’s
height in centimeters by 4 to find their height in Ping-Pong balls;
then, it rounds that answer to the nearest whole number with the
round() function and stores the result in the variable ping_pong_tall.
At z, we do something similar by converting the user’s weight in
kilograms to grams by multiplying by 1,000 and then dividing that
amount by 2.7—the mass in grams of a standard Ping-Pong ball.
That number is rounded to the nearest whole number and stored
in the variable ping_pong_heavy.
At { and |, we do just a little more math by figuring out the
person’s height in feet and inches. As I mentioned previously, this
is normally how we express our height in the United States, and
it will be a nice finishing touch as well as a way for the person to
check that they entered the correct information. The // operator
does integer division, so 66 inches, or 5.5 feet, would result in just 5
being stored in the variable feet, and the % operator (modulo) would
store the remainder, 6 inches. The print statements at } print out
the user’s height and weight, both in standard units and in Ping-
Pong balls.
Here are the results from a few sample runs of the Ping-Pong
calculator program, with Ping-Pong-ball measurements for my
sons, Max and Alex, and me. (The only downside is that now my
kids want to get 31,000 Ping-Pong balls.)
================================ RESTART ================================
Enter your height in inches: 42
Enter your weight in pounds: 45
At 3 feet 6 inches tall, and 45 pounds,
you measure 27 Ping-Pong balls tall, and
you weigh the same as 7576 Ping-Pong balls!