【问题标题】:Making a BMI calculator and trying to call a function with the heights and weights of individuals制作 BMI 计算器并尝试调用具有个人身高和体重的函数
【发布时间】:2021-03-24 10:10:12
【问题描述】:

我正在尝试创建一个 BMI 计算器,并尝试调用一个以身高和体重为参数的函数,并使用公式 weight × 703 / height2 返回 BMI。

recipients = ["John", "Dee", "Aleister", "Lilith", "Paul", "Reggy"]
heights = []
weights = []
for recipient in recipients:
  heights.append(int(input("What is your height " + recipient )))
  weights.append(int(input("What is your weight " + recipient )))

def BMI(weights, heights):
  bmi_total = (weights * 703) / (heights ** 2)

  print(bmi_total)

其余说明如下:

  • 然后应该将该体重指数附加到一个数组中。

  • 使用第二个循环,它应该遍历体重指数数组并调用另一个函数,该函数接受体重指数作为参数并返回个人体重不足、正常体重还是超重。

  • 应统计每个类别中的人数,并显示每个类别中的人数。

  • 您应该决定这六个人的姓名和用于分类的阈值。

我是否为这组指令正确创建了函数?

【问题讨论】:

  • 您的问题可能更适合codereview.stackexchange.com。您应该进行编辑并包括您自己测试的输入和输出。如果您收到错误或不正确的输出,您应该具体说明是什么。

标签: python function


【解决方案1】:

您已经接近了:使用带有zip 的for 循环遍历weights 和`高度列表中的每个weight, height:

recipients = ["John", "Dee", "Aleister", "Lilith", "Paul", "Reggy"]
heights = []
weights = []
for recipient in recipients:
  heights.append(int(input("What is your height " + recipient )))
  weights.append(int(input("What is your weight " + recipient )))

def BMI(weights, heights):
  for weight, height in zip(weights, heights):
    bmi_total = (weight * 703) / (height ** 2)
    print(bmi_total)

BMI(weights, heights)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多