TypeError: unsupported operand type(s) for &: 'float' and 'float'

23,289

& is a bitwise operator, I think you were looking for the boolean and.

But notice that Python also supports the following syntax:

if 18.5 < bmi1 < 24.9:
    # ...

Since you seemed to have trobled with indentation this is how your script might look like:

h = raw_input("Please enter your height in meters: ")
h = float(h)
w_unit = raw_input("Do you want to enter your weight in kg or lbs? ")
w = raw_input("Please enter your weight in {}: ".format(w_unit))
w = int(w)
if w_unit == "kg":
    bmi = w / (h*h)
elif w_unit == "lbs":
    bmi = w / ((h*h) * (39.37 * 39.37) * 703)

print "Your BMI is {:.2f}".format(bmi)
if bmi <= 18.5: 
    print "Your are underweight."
elif 18.5 < bmi <= 25: 
    print "Your weight is normal."
elif 25 < bmi < 30: 
    print "Your are overweight"              
elif bmi >= 30:
    print "Your are obese"

There are a couple of slight improvements:

  • The explicit conversion (since in Python 3 the input function behave like raw_input and there's nothing like the Python 2 input, it might be a good habit to write your input like that)
  • What really changes is the bmi value, so there's no need to write two times the same thing.

Something left to do, might be wrap the whole script into functions :)

Share:
23,289

Related videos on Youtube

user1345589
Author by

user1345589

Updated on October 20, 2020

Comments

  • user1345589
    user1345589 over 3 years

    I wrote this simple program to calculate one's BMI. But I am unable to execute it complete. Below is my program,

    PROGRAM

    h = input("Please Enter your height in meters:")
    q = raw_input("Do you want to enter your weight in kg or lbs?")
    
    if q=="kg":
             w1 = input("Please Enter your weight in kgs:")
             bmi1 = w1/(h*h) 
             print "Your BMI is", bmi1
    
             if bmi1 <= 18.5: 
                            print "Your are underweight."
             if bmi1 > 18.5 & bmi1 < 24.9: 
                                         print "Your weight is normal."
             if bmi1 > 25 & bmi1 < 29.9: 
                                       print "Your are overweight"              
             if bmi1 >= 30: 
                          print "Your are obese"                    
    
    
    if q=="lbs":
              w2 = input("Please Enter your weightin lbs:")
              bmi2 = w2/((h*h)*(39.37*39.37)*703) 
              print "Your BMI is:", bmi2
    
              if bmi2<= 18.5: 
                            print "Your are underweight."
              if bmi2>18.5 & bmi2<24.9: 
                                      print "Your weight is normal."
              if bmi2>25 & bmi2<29.9: 
                                    print "Your are overweight"         
              if bmi2>=30: 
                         print "Your are obese" 
    

    OUTPUT

    Please Enter your height in meters:1.52
    Do you want to enter your weight in kg or lbs?kg
    Please Enter your weight in kgs:51
    Your BMI is 22.074099723
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "bmi.py", line 11, in <module>
        if bmi1 > 18.5 & bmi1 < 24.9: 
    TypeError: unsupported operand type(s) for &: 'float' and 'float'
    

    Where am I going wrong? Anyone just let me know..

    Thanks :).

  • user1345589
    user1345589 almost 12 years
    Well I had tried the above syntax before, but I guess I had not indented the code correctly. Is there any reference that would help me regarding the indentation specifics for python.
  • Rik Poggi
    Rik Poggi almost 12 years
    @user1345589: I'm not aware of a specific source, I'll have to google it like you. Anyway, in brief, the point is to indent four spaces every block. You can follow the official coding style guide: PEP8.
  • Rik Poggi
    Rik Poggi almost 12 years
    @user1345589: Since you seemed interested I edited my answer to show you how your program might look like :) If you want reviews on working code you'll find good advice on codereview. I'm looking forward to have this answer accepted :)