BMI计算器不让转换器选择一个或另一个



我正在制作BMI计算器,我正在尝试将转换器添加到不知道其SI比例的人的代码中。问题是我要写这篇文章的方式是,如果您知道您的SI,请按Enter,它会要求您提供SI首选项。

print("n")
print("     -------------------------------          -------------------------------     ")
print("                                 BMI Calculator                                 ")
print("This calculator uses SI unites to determine BMI, use the converters below for pounds and feet.")
print("               If you know the SI units for just press enter.              ")
print("     -------------------------------          -------------------------------     ")
none=()
c1=input("Please state your Weight in Pounds: ")
if c1 is none: 
    f1=float(c1)
    w1=f1*.45659237
c2=input("Please state your Height in Feet: ")
if c2 is none:
    f2=float(c2)
    h1=f2*.3048
if c1 is not none and c2 is not none:
    B=(w1/h1**2)
    print(B)
else:
    print("n")
    print("Great you know your SI Preferances: ")
    W=input(("Please state your Weight in Kg: "))
    H=input(("Please state your Height in m: "))
    h=float(H)
    w=float(W)
    B=(w/h**2)
    print(B)

我设置了NONE =(),以便如果您输入一无所有,则将进入下一个问题。从本质上讲,如果您知道SI的比例与身体的比例,则可以使用。如果您按Enter并输入SI单元,则可以正常工作。如果您输入磅和脚,它会忽略它并继续前进。我想知道循环在哪里出错。

这是我尝试运行此

时遇到的错误
B=(w1/h1**2)
NameError: name 'w1' is not defined

您可以将W1和H1声明为Globals,并使用!=(不是等于)比较的空字符串的用户输入。看看比较操作员。

if c1 != '': global w1, h1 f1=float(c1) w1=f1*.45659237

还考虑使用格式方法将结果降低到2个小数点

print("Your bmi:{0:.2f}".format(B))

最新更新