w3resource

Python: Convert feet and inches to centimeters

Python Basic: Exercise-59 with Solution

Write a Python program to convert height (in feet and inches) to centimeters.

Pictorial Presentation:

Convert height (in feet and inches) to centimeters

Sample Solution:-

Python Code:

print("Input your height: ")
h_ft = int(input("Feet: "))
h_inch = int(input("Inches: "))

h_inch += h_ft * 12
h_cm = round(h_inch * 2.54, 1)

print("Your height is : %d cm." % h_cm)

Sample Output:

Input your height: 
Feet:  5
Inches:  3
Your height is : 160 cm.

Explanation:

h_ft = int(input("Feet: "))
h_inch = int(input("Inches: "))

In the above code -

The input() function is used to prompt the user to enter their height in feet and inches, and the int() function is used to convert the input to integer values for h_ft (height in feet) and h_inch (height in inches).

h_inch += h_ft * 12: The height in inches is then calculated by adding h_ft * 12 to h_inch, which converts the height to inches.

h_cm = round(h_inch * 2.54, 1): The height in centimeters is calculated by multiplying the height in inches by 2.54 (the conversion factor from inches to centimeters), and rounding the result to one decimal place using the round() function.

Finally, the print() function prints a message to the console in the format "Your height is : height_cm cm".

Flowchart:

Flowchart: Convert height (in feet and inches) to centimeters.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to sum of the first n positive integers.
Next: Write a Python program to calculate the hypotenuse of a right angled triangle.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value:

Example:

def tips_map_values(obj, fn):
  ret = {}
  for key in obj.keys():
    ret[key] = fn(obj[key])
  return ret
users = {
  'Owen': { 'user': 'Owen', 'age': 29 },
  'Eddie': { 'user': 'Eddie', 'age': 15 }
}

print(tips_map_values(users, lambda u : u['age'])) # {'Owen': 29, 'Eddie': 15}

Output:

{'Owen': 29, 'Eddie': 15}

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook