## Convert a persons height from the format ft'in" to just inches

I have a project in my computer science class and part of it needs to know the height of a person. I want it to ask "What is your height?(format like ft'in"): " and convert the input into just inches but I have no clue how to do this. Any help?

You could use `regex`

to parse any variant of the `ft' in"`

format:

import re pattern = re.compile(r"""(\d+)' *(\d+)(?:"|'')?""") text = input("What is your height? ") feet, inches = map(int, re.match(pattern, text).groups())

Here's what the regex `(\d+)' *(\d+)(?:"|'')?`

does: it matches a (nonempty) group of digits on the left of an apostrophe, followed by zero or more spaces, and another group of digits, followed by an optional double-apostrophe (either `"`

or `''`

). As a note, since we need a literal `"`

in the pattern string, it is enclosed in triple double-quotes.

The advantage of using a regex is that if your format becomes more complex later (e.g., variable number of space in between the apostrophe, possibly missing apostrophe, etc.), it's much easier to adapt since you already have everything in place. Also, you can even do some error-checking:

import re heightPattern = re.compile(r"""(\d+)' *(\d+)(?:"|'')?""") def parseHeight() -> (int, int): text = input("What is your height? ") match = re.match(heightPattern, text) if not match: raise ValueError("Couldn't parse height") feet, inches = map(int, match.groups()) return feet, inches

Finally, you can convert it all to inches simply by doing

inches += 12*feet

and thus the total number of inches will be stored in `inches`

.

**Height Converter ft and in to cm,** Convert to cm from ft and in. Convert height measurements from US units of feet and inches to metric units of centimeters. Enter feet and/or inches to convert to� Am using an external API that requests a person's height in inches, so 5.10" (5 feet and 10 inches) would be 70", now i want to give the user an input box so they can type 5.10 and then convert it to 70 in order to pass it to the API. I think there is a straight forward solution which is to: truncate input towards 0; convert it from feet to inches

I think what you're asking is "how do I take their textual input and turn it into numbers?"

I also have no idea what language you're using, but let's assume you're using Python, from the tag you put on the thing.

My favorite way to parse input is using regular expressions, see the re library.

Something like:

import re txt = input("What is your height? (format like ft'in\"):") search_results = re.search("^([0-9]+)'([0-9]+)\"$") if search_results: print("feet: " + int(search_results.group(1)) print("inches: " + int(search_results.group(2)) else: print("You didn't format it correctly")

Full disclosure, I haven't tried the above code for correctness or run it. Just as a pointer in the right direction :)

**US Height Conversion Calculator,** Converts between U.S. customary and metric units of height. Metric. m. cm. = mm. = cm. = m. US customary. ft. in. = in. = ft� 1. When converting height to cm, the number of feet is multiplied by 12 and added to inches to get the total inches. This is then multiplied by 2.54 to get height in cm. When converting height to feet inches, the height converter divides by 2.54 to get inches. Next, this total number of inches is divided by 12 to get the number of feet.

You can get as a string and split to feet and inches

text = input("Enter your height: ") # Format should be <int>"<int>' feet = int(text.split("\"")[0]) inch = int(text.split("\"")[1].split("'")[0])

**Python: Convert height (in feet and inches) to centimeters ,** Here is a code that takes the height as one number (i.e. 5.9), separates it into ft and in and then converts it. def ft_to_cm(imp): dec_inch = imp - int(� With the standard unit, representing feet and inches together, such as a person's height, can get tricky. For example, if you want to create a table to list a group of people's heights and you want to put it in the feet and inches format, you may want to consider using a custom function.

Here is a very simple Python3 code **without** regex:

h = input('Enter height in ft\'in": ') # ask for input h_split = h.split('\'') # break string into 2 parts where single quote occurs ft, inch = (h_split[0], h_split[1].split('"')[0]) # extract inches in a similar way ft = int(ft) # convert feet to integer inch = int(inch) # convert inches to integer height = ft*12 + inch # convert feet to inches and add inches print(f'Height in inches: {height}') # For Python2 use print 'Height in inches: ' + str(height)

Good luck!

**Ex: Convert Height in Feet and Inches to Inches, Centimeters, and ,** This video explains how to convert a height given in feet and inches to Your browser does Duration: 5:43
Posted: Oct 31, 2012 Conversion of the Imperial system measurement for the shoulder height of Kooikerhondje (spaniel breed). [9] 2016/10/02 12:22 Male / 40 years old level / Self-employed people / Useful / Purpose of use

You absolutely don't need regexes to parse that. Just `split`

the string!

text = input("Input height:") Ft, In = text.split("'") # "1'2\"" -> ['1', '2"'] result = int(Ft) * 12 + int(In.strip()[:-1]) # get rid of the trailing double quote with `[:-1]` print(f'You are {result} inches tall')

**How to Calculate Height in Centimeters,** For example, a 5' 7" person is (5 � 12) + 7 = 67" tall. To convert inches to centimeters (in to cm), simply multiply by 2.54. This is because there are exactly 2.54 cm� Convert foot, inch and fraction of an inch to decimal format. Example: 7'4 5/16" # of feet = 7' # of inches = 4" Fractional Inch = 5/16" 7'4 5/16" in decimal format = 7.359 Convert Inch and Fraction to decimal format. This calculator converts inch and fractions of an inch to decimal format.

Ft = 0 Else ‘ Foot symbol (‘) has been found ‘ Given foot symbol has been found, check that – if an inch symbol is also within the string – the inches and feet are separated using the FtInchSeparator character(s) If InchSymLoc <> 0 And FtInchSeparatorLoc = 0 Then StringToFt = “Invalid input format: The character(s) used to separate

I still struggle with feet and inches. Lately, one of my colleagues told me he lost a 'stone' and I had to make all sorts of clarifying questions. I know that 5' is short and 6' is tall, but I really don't know how much. I agree that describing the person is more useful. – Thanasis Karavasilis Mar 6 '17 at 10:43

# Python program to convert height (in feet and inches) to centimeters height = input ("Enter your height in feet and inches:- ") feet = height.split ("`") inches = height.split ("`",maxsplit=-1)

##### Comments

- There are 12 inches in 1 foot
- If you search on the phrase "Python convert feet to inches", you’ll find resources that can explain it much better than we can in an answer here.
- Note the the question asks "convert the input into just inches".
- Another has commented that there are twelve inches to the foot. I'm sure the original asker is capable of that part.
- What about the trailing
`"`

at the end of the inches? You are forgetting about that. - @djhaskin987, oops, fixed. Thanks!