# This program calculates a person's BMI after
# user inputs their height and weight, it then converts
# the data from inches to meters and pounds to kilograms.
# Based on findings the program displays persons BMI
# Display welcome message.
print( "Welcome to My BMI Calculator \n " )
# Ask the user to input their name.
persons_name = input( 'Name of person that we are calculating the BMI for: ' )
# Ask for the user's height in inches.
inches = float(input( 'Supply Height in Inches: ' ))
# Ask for the user's weight in pounds.
pounds = int(input( 'Supply Weight in Pounds: ' ))
# Convert from inches to meters
inches_to_meters = float(inches / 39.36)
# Convert from pounds to kilograms
pounds_to_kilograms = float(pounds / 2.2)
# Calculate the person's BMI
BMI = float(pounds_to_kilograms / ( inches_to_meters * inches_to_meters ))
# Display the BMI
print( persons_name, 'BMI is:' , format(BMI, '.2f' ))
# Display person's BMI findings based on the given data
if BMI <= 18.50:
print( 'BMI finding is the subject is: Underweight ' )
elif BMI > 18.51 < 24.90:
print( 'BMI finding is the subject is: Normal ' )
elif BMI > 24.91 < 29.90:
print( 'BMI finding is the subject is: Overweight ' )
elif BMI > 29.90:
print( 'BMI finding is the subject is: Obese ' )
I apologize in advance if I formatted this code wrong after pasting it here. If its wrong, please let me know, so I learn how to properly format it on this website. From what I understand, I indent each line by 4 spaces.
Here is a BMI analyzer program that takes person's height in inches and converts it to meters, takes the persons weight in pounds and converts it to kilograms.
After testing it, it seems to work, but only if you input whole numbers. So if you enter 0 for weight or 0 for height, there wont be an error, instead it will use that number in the BMI calculation.
My question is: How do I make sure the user inputs only real numbers, no negatives, or numbers with decimal points, and if they do, to display and error to say "Use whole number only"