YouTube Video Tutorial How to write a Python program to convert centimetres to inches
Formula to convert centimetres to inches
Explanation of this Python Program
This Python program prompts the user to enter a height in inches, converts it to centimeters, and then prints the converted value. It uses the conversion factor of 2.54 to perform the conversion and rounds the result to two decimal places before displaying it to the user.
Here's an explanation of the provided Python program line by line:
pythoninches = float(input('Enter the Height in inches to convert into centimeters:'))
- This line prompts the user to enter a height in inches and assigns the entered value to the variable
inches
. - The
input()
function is used to receive input from the user, andfloat()
is used to convert the input into a floating-point number.
pythoncentimeters = inches * 2.54
- This line calculates the equivalent value in centimeters by multiplying the
inches
variable by the conversion factor 2.54. - The result is assigned to the variable
centimeters
.
pythoncentimeters = round(centimeters, 2)
- This line rounds the value in
centimeters
to two decimal places using theround()
function. - The rounded value is then assigned back to the
centimeters
variable.
pythonprint(inches, 'Inches =', centimeters, 'Centimeters')
- This line displays the result of the conversion to the user.
- The
print()
function is used to output a formatted string that includes the original inches value, the equal sign, and the converted centimeters value.
Comments