我试图让程序告诉我,根据用户的输入,有多少人属于“体重不足”、“正常体重”和“超重”类别。每次我运行它,都会得到0的响应。有人能告诉我我做错了什么吗
def under(BMIs):
return BMIs < 25
def normal(BMIs):
return BMIs >= 25 and BMIs < 35
def over(BMIs):
return BMIs > 35
print ("This program will help calculate the body mass index of 6 people")
individuals = list()
for i in range(6):
user = str(input("Please enter the name of one of the individuals: "))
individuals.append(user)
BMIs = list()
for user in individuals:
print("Calculating for", user)
height = int(input(user + ", in inches, how tall are you? "))
weight = int(input(user + ", in pounds, how much do you weight? "))
BMIs.append(weight * 703/height**2)
for i, bmi in enumerate(BMIs):
if under(bmi):
print (individuals[i], "is underweight")
elif normal(bmi):
print (individuals[i], "is normal weight")
elif over(bmi):
print (individuals[i], "is overweight")
count1 = BMIs.count(under)
count2 = BMIs.count(normal)
count3 = BMIs.count(over)
print ("The number of under weight individuals is: ", count1)
print ("The number of normal weight individuals is: ", count2)
print ("The number of over weight individuals is: ", count3)
线路
正在计算函数
under
本身在BMIs
列表中出现的次数,而不是BMIs
列表中有多少元素进入该函数后计算为True
这里有一个想法可以改变这一点:每当函数
under
对给定的bmi计算为True
时,我们将1添加到另一个列表中,然后简单地询问列表的长度:另一个解决方案是使用列表理解并计算每个函数返回
True
的BMI数-这更简单,但您可能还不熟悉语法:希望这足够清楚,以便您理解为什么它也发生在
count2
和count3
上就像@jfaccioni所说的
线路
正在计算函数
under
本身在BMIs列表中出现的次数,而不是BMIs列表中有多少个元素进入该函数后计算为True
然而,我对@jfaccioni有一种稍微不同的方法
相关问题 更多 >
编程相关推荐