【问题标题】:Conditional Loops with arrays带数组的条件循环
【发布时间】:2021-03-25 18:43:40
【问题描述】:

在我的代码中,我尝试使用第二个循环,它应该遍历体重指数数组并调用另一个函数,该函数接受体重指数作为参数并返回个人是否体重过轻、正常体重或超重。

这就是我卡住的地方。代码.....

individuals = ["Mike Jackson", "Mike Tyson", "Mike Jordan"]
bodymass = list()
index = 0

def BMI(height, weight):
    bmi = (weight * 703) / (height ** 2)
    return bmi

for individual in individuals:
    print("Do you want to know the BMI for", individual, "?")
    print("The weight is required for", individual)
    weight = eval(input("Enter weight in pounds here:  "))
    print("The height is required for", individual)
    height = eval(input("Enter height in inches for here:  "))
    bmi = BMI(height, weight)
    bodymass.append(bmi)
    print("The body mass index for", individual, "is:  ", (bmi))

for i in range(len(bodymass)):
    if 18.5 > bodymass[i]:
        print("You are underweight.")
    elif 18.5 <= bodymass[i] < 25:
        print("Your weight is normal.")
    elif bodymass[i] >= 25:
        print("You are overweight.")

==============================================

这是结果...

Do you want to know the BMI for Mike Jackson ?
The weight is required for Mike Jackson
Enter weight in pounds here:  175
The height is required for Mike Jackson
Enter height in inches for here:  72
The body mass index for Mike Jackson is:   23.73167438271605
Your weight is normal.
Do you want to know the BMI for Mike Tyson ?
The weight is required for Mike Tyson
Enter weight in pounds here:  184
The height is required for Mike Tyson
Enter height in inches for here:  80
The body mass index for Mike Tyson is:   20.21125
Your weight is normal.
Your weight is normal.
Do you want to know the BMI for Mike Jordan ?
The weight is required for Mike Jordan
Enter weight in pounds here:  215
The height is required for Mike Jordan
Enter height in inches for here:  78
The body mass index for Mike Jordan is:   24.843030900723207
Your weight is normal.
Your weight is normal.
Your weight is normal.

Process finished with exit code 0

=====================================

我遇到的问题是下一个输入值的人或对象,前一个人的结果显示。因此,第三人称显示“您体重不足”的 3 个值,而不是仅显示该人的结果。

您体重过轻。 你体重过轻。 你体重过轻。

【问题讨论】:

    标签: python arrays python-3.x list numpy


    【解决方案1】:

    由于(for)函数,在每一轮中,所有的结果都会被打印出来; 为解决此问题,创建一个函数并在其中键入代码的第二个块,并在您的第一个 (for) 函数的每一轮中,调用此函数,如下所示:

    def bmi_result(bmi) :
        if 18.5 > bmi:
           print("You are underweight.")
        elif 18.5 <= bmi < 25:
           print("Your weight is normal.")
        elif bmi >= 25:
           print("You are overweight.")
     
    

    使用 bmi_result(bmi) 代替 bodymass.append(bmi) 祝你好运。

    【讨论】:

    • 那没有用,它仍然显示下一个结果。
    • 您的体重正常。你的体重是正常的。您的体重正常。
    【解决方案2】:

    使用这样的代码。 当您输入体重和身高时,它会为您提供该人的 BMI 和结果 然后你输入下一个人的信息,它会再次给你 BMI 和结果。 代码:

    individuals = ["Mike Jackson", "Mike Tyson", "Mike Jordan"]
    bodymass = list()
    index = 0
    
    def BMI(height, weight):
        bmi = (weight * 703) / (height ** 2)
        return bmi
    
    
    def bmi_result(bmi) :
        if 18.5 > bmi:
           print("You are underweight.")
        elif 18.5 <= bmi < 25:
           print("Your weight is normal.")
        elif bmi >= 25:
           print("You are overweight.")
    
    for individual in individuals:
        print("Do you want to know the BMI for", individual, "?")
        print("The weight is required for", individual)
        weight = eval(input("Enter weight in pounds here:  "))
        print("The height is required for", individual)
        height = eval(input("Enter height in inches for here:  "))
        bmi = BMI(height, weight)
        bmi_result(bmi)
        print("The body mass index for", individual, "is:  ", (bmi))
    
       
       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 2022-10-31
      • 2017-07-11
      相关资源
      最近更新 更多