当循环未执行时,defhealthindex函数不会发生

2024-04-19 19:29:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我无法使healthindex函数工作。即使输入b或h,while循环也不会执行。我试着让输入根据输入产生两段独立的代码,但事实并非如此。谢谢大家!

def inputM():
  print("Enter weight in kg")
  weightm = float(input())
  print("Enter heigh in meters")
  heightm = float(input())
  return weightm, heightm
def inputI():
  print("Enter weight in pounds")
  weighti = float(input())
  print("Enter height in inches")
  heighti = float(input())
  return weighti, heighti

def healthindex (BMIList, BMINum, bmi):
  if healthy == "b": 
    print (str(bmi))
  elif healthy == "h":
    index = 0
    print ("Your bmi is" + (str(bmi))
    while index < len(BMIList):
      if bmi < BMINum[index]:
        print ("And, you are " + BMIList[index])
        return
      index = index + 1
    print("You are Obese")
  return

BMIList = ["severly underweight", "underweight", "healthy", "overweight", "obese"]
BMINum = [12, 18.4, 24.9, 29.9, 200]
print("Welcome to BMI Calculator!")
print("Enter I for Imperial or M for Metric")
request = input().upper()

if request == "M":
  weightm, heightm = inputM()
  bmi = weightm/(heightm**2)
elif request == "I":
  weighti, heighti = inputI()
  bmi = (703*weighti)/(heighti**2)  
else:
  print("Invalid input")

print("Enter b to only see your bmi or enter h if you would like to see your bmi and health index")

healthy= input()
healthindex (BMIList, BMINum, bmi)

2条回答

您还应该将Health变量传递给healthindex函数

healthindex (BMIList, BMINum, bmi, healthy)

完整代码

def inputM():
  print("Enter weight in kg")
  weightm = float(input())
  print("Enter heigh in meters")
  heightm = float(input())
  return weightm, heightm
def inputI():
  print("Enter weight in pounds")
  weighti = float(input())
  print("Enter height in inches")
  heighti = float(input())
  return weighti, heighti

def healthindex (BMIList, BMINum, bmi, healthy):
  if healthy == "b": 
    print (str(bmi))
  elif healthy == "h":
    index = 0
    print ("Your bmi is " + (str(bmi)))
    while index < len(BMIList):
      if bmi < BMINum[index]:
        print ("And, you are " + BMIList[index])
        return
      index = index + 1
    print("You are Obese")
  return

BMIList = ["severly underweight", "underweight", "healthy", "overweight", "obese"]
BMINum = [12, 18.4, 24.9, 29.9, 200]
print("Welcome to BMI Calculator!")
print("Enter I for Imperial or M for Metric")
request = input().upper()

if request == "M":
  weightm, heightm = inputM()
  bmi = weightm/(heightm**2)
elif request == "I":
  weighti, heighti = inputI()
  bmi = (703*weighti)/(heighti**2)  
else:
  print("Invalid input")

print("Enter b to only see your bmi or enter h if you would like to see your bmi and health index")

healthy= input()
healthindex (BMIList, BMINum, bmi, healthy)
...
def healthindex (preference, bmi):
  if preference == "b": 
    print (str(bmi))
  elif preference == "h":
    index = 0
    print ("Your bmi is" + (str(bmi)))
    while index < len(BMIList):
      if bmi < BMINum[index]:
        print ("And, you are " + BMIList[index])
        return
      index = index + 1
    print("You are Obese")
  return

...
healthindex (healthy, bmi)

...表示未更改的代码

相关问题 更多 >