Reputation: 87
Here is the problem:
Given that 1 foot = .3048 meters, write a Python script that will convert a height given meters to a height given in feet and inches.
I looked over the notes the professor gave that day and I am following each step but my program just doesn't seem to work correctly. Here's what I have:
print("This program will convert a height given meters to a height given in feet and inches.")
meters = float(input("Enter height in meters:"))
meters_in_ft = meters // .3048
meters_in_in = meters_in_ft % 12
print("The height is", meters_in_ft,"feet and",meters_in_in, "inches")
When I run the program and type certain meters I will get it correct in feet, but a lot of times the measurement in inches is wrong.
Upvotes: 6
Views: 25008
Reputation: 330
Quick answer for other visitors from a Google search:
Meters to feet + inches
feet = int(meters / 0.3048)
inches = meters / 0.3048 % 1 * 12
inches = round(inches) #optional
Feet and inches to meters
meters = (feet + inches/12)*0.3048
Upvotes: 0
Reputation: 780
To get the inches portion from meters, you need to get the decimal portion of meters / .3048
by using % 1
. Then you can convert that to inches by multiplying by 12
.
inches = meters / .3048 % 1 * 12
Upvotes: 9