简体   繁体   English

类型错误:& 不支持的操作数类型:'float' 和 'float'

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

I wrote this simple program to calculate one's BMI.我编写了这个简单的程序来计算一个人的 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 :).谢谢 :)。

& is a bitwise operator , I think you were looking for the booleanand . &是一个按位运算符,我认为您正在寻找布尔值and

But notice that Python also supports the following syntax:但请注意,Python 还支持以下语法:

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)显式转换(因为在 Python 3 中input函数的行为类似于raw_input并且没有像 Python 2 input那样的东西,这样编写输入可能是一个好习惯)
  • What really changes is the bmi value, so there's no need to write two times the same thing.真正改变的是bmi值,所以没有必要写两次相同的东西。

Something left to do, might be wrap the whole script into functions :)剩下要做的事情,可能是将整个脚本包装成函数:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: 不支持的操作数类型 -: &#39;float&#39; 和 &#39;NoneType&#39; - TypeError: unsupported operand type(s) for -: 'float' and 'NoneType' TypeError:-:“ float”和“ method”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'float' and 'method' TypeError:*:“ dict”和“ float”不支持的操作数类型 - TypeError: unsupported operand type(s) for *: 'dict' and 'float' TypeError:-:“ float”和“ FloatVector”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'float' and 'FloatVector' TypeError: *: 'float' 和 'set' 不支持的操作数类型 - TypeError: unsupported operand type(s) for *: 'float' and 'set' TypeError: 不支持的操作数类型 -: 'float' 和 'str' - TypeError: unsupported operand type(s) for -: 'float' and 'str' 类型错误:* 不支持的操作数类型:'_Printer' 和 'float' - TypeError: unsupported operand type(s) for *: '_Printer' and 'float' TypeError:+不支持的操作数类型:“ float”和“ str” - TypeError: unsupported operand type(s) for +: 'float' and 'str' TypeError不支持%:Float和NoneType的操作数类型 - TypeError unsupported Operand type(s) for %: Float and NoneType TypeError:&lt;&lt;:&#39;int&#39;和&#39;float&#39;不受支持的操作数类型 - TypeError: unsupported operand type(s) for <<: 'int' and 'float'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM