0

I'm trying to do this exercise for school, but the code won't work, precisely it doesn't execute if statements, after calculating BMI. Further if the input is not correct, the except statment is now checked. Please advise what to correct in the code. Thanks!

user_input_1 = input('What is your weight')
user_input_2 = input('What is your height')
b = 'BMI'
b = int(user_input_1)/(float(user_input_2)**2)
while True:
  try:
     user_input_1 == int and user_input_1 > 0
     user_input_2 == float and user_input_2 > 0
     print(b)
     if b in range(1, 19):
        print('You are skinny as a rail')
        break
     if b in range(19, 26):
        print('You are fit as a butcher\'s dog')
        break
     if b >= 25:
        print('You are as plum as a partridge')
        break
     break
  except ZeroDivisionError:
     input('Enter your height in meters as a float')
  except user_input_1 != int:
     input('Please enter your weight in kg')

3 Answers 3

2

for start: note that you declare the variable b row after another. this means that the first deceleration is redundant. second, don't use break, but elif instead of the 'if'. third, the first two rows after try, do nothing..

Sign up to request clarification or add additional context in comments.

Comments

2

Where to start?

  1. The only division takes place before entering the try block, so your except ZeroDivisionError will never get triggered.
  2. except user_input_1 != int evaluates to except True which is meaningless and will never get triggered
  3. The only way you don't hit a break in your while loop, is if you throw an exception that gets caught (if it's not caught, it'll escape your while loop and exit your program). Since the code that gets user input is outside of the while loop, there would be (if the exceptions could ever be caught) no difference and you'd see the error messages just repeating forever.

There seem to be some fundamental gaps in your understanding of python. I'd suggest filling those first; try and implement just a single desired feature (for example, take user input and display suitable error message if it is invalid) and build from there.

Comments

1

There are lots of problems with your code as SpoonMeiser already mentioned:

  1. The only division takes place before entering the try block, so your except ZeroDivisionError will never get triggered.
  2. except user_input_1 != int evaluates to except True which is meaningless and will never get triggered
  3. The only way you don't hit a break in your while loop, is if you throw an exception that gets caught (if it's not caught, it'll escape your while loop and exit your program). Since the code that gets user input is outside of the while loop, there would be (if the exceptions could ever be caught) no difference and you'd see the error messages just repeating forever.

Other errors are:

  1. The use of b in range(x, y): These only include integer values values in that interval. And you can test it with:

    • print(2.1 in range(0,10)) # Prints: False
    • print(2 in range(0,10)) # Prints: True
  2. You should use = float(input(...)) from the start: if you'll always will use the user input as a float just do it once.

  3. b = 'BMI' ?


Here is the resulting code:

 def foo():
    try:
        user_input_1 = float(input('What is your weight? '))
        user_input_2 = float(input('What is your height? '))
        if all(x>0 for x in [user_input_1,user_input_2]):
            b = user_input_1/user_input_2**2
            print(b)
            if 0 > b > 26:
               print('You are skinny as a rail')
            elif 19 > b > 26:
               print("You are fit as a butcher's dog")
            else:
               print('You are as plum as a partridge')
        else: raise ValueError
    except ValueError:
        print('Enter your height in meters as a float')
        foo()

1 Comment

We shouldn't be doing people's homework for them, rather, just giving pointers. Also, looping is probably better than recursion in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.