1
flag='yes'
#while loop for reccursion
while flag != 'no':
    print ("\n BMI Calculator")
    #Exception block for catching non integer inputs
    try:
        #Prompting user to input weight
        weight = int(input('Enter your weight in pounds : '))
        while weight == 0:
            sys.exit()
    except ValueError:
        print ('Oops!! Kindly enter only numbers.')
        flag = input('Do you want to try again? yes/no : ')
        continue

    try:
        #Prompting user to input height
        height = float(input('Enter your height in inches : '))
        while height == 0:
            sys.exit()
    except ValueError:
        print ('Oops!! Kindly enter only numbers.')
        flag = input('Do you want to try again? yes/no : ')
        continue

    #Function for calculating BMI    
    def bmi_calculator():
        #Exception block for catching division by zero
        try:
            #Formula for calculating BMI
            BMI = round(weight * 703/(height*height), 2)
            return (BMI)
        except ZeroDivisionError:
            print ('Oops!! Cannot divide by zero.')
            sys.exit()

Is there any exception handling available for accepting only non zero inputs? Is there any way to avoid another while or if loop?

James Z
  • 12,209
  • 10
  • 24
  • 44
Varun
  • 117
  • 1
  • 4
  • 14
  • This isn't exactly what you want, but you can throw an exception if a value is zero. This will require an if statement though. An example is given [here](https://stackoverflow.com/questions/15099379/limit-input-to-integer-only-text-crashes-python-program) – David Jun 23 '17 at 03:34
  • Why don't you want to use an if loop? Also, why auto exit for == 0? – toonarmycaptain Jun 23 '17 at 03:49
  • I was thinking using while and if would increase the number of lines and code but "if weight == 0: raise ValueError" This serve my purpose without adding extra lines so its fine. – Varun Jun 23 '17 at 05:08
  • Maybe you want to prevent negative numbers too. – Marichyasana Jun 23 '17 at 06:36
  • @Marichyasana. yup, I used "if weight <= 0" to prevent negative numbers. – Varun Jul 03 '17 at 14:53

1 Answers1

0

If you want to handle 0 numbers properly one of the ways is to raise exception when 0 is entered:

#Function for calculating BMI    
def bmi_calculator():
    flag='yes'
    while flag != 'no':
        print ("\n BMI Calculator")
        #Exception block for catching non integer inputs
        try:
            #Prompting user to input weight
            weight = int(input('Enter your weight in pounds : '))
            if weight == 0:
                raise ValueError
        except ValueError:
            print ('Oops!! Kindly enter non-zero numbers only.')
            flag = input('Do you want to try again? yes/no : ')
            continue

        try:
            #Prompting user to input height
            height = float(input('Enter your height in inches : '))
            if height == 0:
                raise ValueError
        except ValueError:
            print ('Oops!! Kindly enter non-zero numbers only.')
            flag = input('Do you want to try again? yes/no : ')
            continue

        #Formula for calculating BMI
        BMI = round(weight * 703/(height*height), 2)
        return (BMI)

print(bmi_calculator())

Also I replaced your while weight == 0 and while height == 0 by if statements - they look more appropriate and I placed the main while loop inside of the function defintion bmi_calculator so that it can be called whenever you need it.

Nurjan
  • 5,889
  • 5
  • 34
  • 54