3
# 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"

asynchronos
  • 583
  • 2
  • 14
silva0003
  • 41
  • 4

3 Answers3

0

Get rid of float() conversion of input and try:

x=input()
try: 
   int(x) 
except: 
   raise ValueError('Cannot accept:', x)

Also highly recommended split your code to functions.

Evgeny
  • 4,173
  • 2
  • 19
  • 39
0

A simple while loop should work for you:

inches=float(input('Height in inches:'))
while (inches<0)|(inches%1!=0):
    inches=float(input('error, use whole numbers only:'))
inches = int(inches)

Height in inches:5.2

error, use whole numbers only:-182

error, use whole numbers only:180
180
Todd Muller
  • 11
  • 2
  • 7
0

Use try...except statements.
These statements are used to try a piece of code that the programmer thinks may have errors. The programmer then 'catches' the error, if any.
In your case, the numbers entered should be natural (not whole numbers, because BMI calculation with height as zero is not correct).

The code will, therefore, be:

print("Welcome to My BMI Calculator.\n")
persons_name = input("Name of person that we are calculating the BMI for: ")
try:
    inches = int(input("Enter height in inches: "))
    assert inches > 0
    inches_to_meters = inches / 39.36
    pounds = int(input("Enter weight in pounds: "))
    assert pounds > 0
    pounds_to_kilograms = pounds / 2.2
    BMI = pounds_to_kilograms / (inches_to_meters ** 2)
    print(persons_name, "BMI is:", format(BMI, ".2f" ))
    if BMI <= 18.50:
        print( 'BMI finding is the subject is: Underweight ' )
    elif 18.51 < BMI <  24.90:
        print( 'BMI finding is the subject is: Normal ' )
    elif 24.91 < BMI <  29.90:
        print("BMI finding is the subject is: Overweight")
    elif BMI > 29.90:
        print("BMI finding is the subject is: Obese")
except ValueError:
    print("Enter only natural numbers!")
except AssertionError:
    print("Enter positive numbers only!")
NameError
  • 206
  • 1
  • 3
  • 19