我的bmi计算器正在倒退

时间:2018-06-10 16:52:56

标签: python python-3.x

我为学校写的我的Bmi计算器正在向后生成输出,它首先需要用户信息和名称信息。请帮助,我需要它反过来。

user = str
end = False

def bmi_intro():
    print("BMI Calculator")
    while end == False:


        user = input("Enter students name or '0' to quit: ")
        if user == "0":
            print("end of report!")
        else:
            def userName(str):
                user = str
            print("Lets gather your information,", user)
            break

    get_height = float(input("Please enter your height in inches: "))
    get_weight = float(input("Please enter your weight: "))
    body_mass_index = (get_weight * 703) / (get_height ** 2)
    print ("Your bmi is: ", body_mass_index)

def main():
  get_height = 0.0
  get_weight = 0.0
  body_mass_index = 0.0
bmi_intro()

2 个答案:

答案 0 :(得分:1)

您的代码中存在许多问题:

  • 您尚未设置end
  • 的值
  • 您没有正确缩进
  • main功能在这种情况下是多余的

应该如下:

def bmi_intro():
  end = False

  print("BMI Calculator")

  while end == False:

      user = input("Enter student's name or '0' to quit: ")
      if user == "0":
          print("end of report!")
          end = True
      else:
          print("Lets gather your information,", user)

          get_height = float(input("Please enter your height in inches: "))
          get_weight = float(input("Please enter your weight: "))
          body_mass_index = (get_weight * 703) / (get_height ** 2)

          print ("Your bmi is:", body_mass_index)

bmi_intro()

其他建议

您可能希望在问题中指出衡量单位的重量,即:

get_weight = float(input("Please enter your weight in pounds (lbs): "))

除非您计划扩展此代码和/或添加其他功能,否则不需要功能。如果愿意,您可以取消函数定义和函数调用。

答案 1 :(得分:0)

更正缩进和删除break语句可以解决您的问题(我尝试尽可能少地编辑您的代码,我认为理解代码会对您有所帮助):

user = str
end = False

def bmi_intro():
        print("BMI Calculator")
        while end == False:


        user = input("Enter students name or '0' to quit: ")
        if user == "0":
            print("end of report!")
            break
        else:
            def userName(str):
                user = str
            print("Lets gather your information,", user)


            get_height = float(input("Please enter your height in inches: "))
            get_weight = float(input("Please enter your weight: "))
            body_mass_index = (get_weight * 703) / (get_height ** 2)
            print ("Your bmi is: ", body_mass_index)

def main():
  get_height = 0.0
  get_weight = 0.0
  body_mass_index = 0.0
bmi_intro()