对发生的次数进行分类

2024-09-28 01:31:19 发布

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

我试图让程序告诉我,根据用户的输入,有多少人属于“体重不足”、“正常体重”和“超重”类别。每次我运行它,都会得到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)


Tags: ofinforreturnisdefoverprint
2条回答

线路

count1 = BMIs.count(under)

正在计算函数under本身BMIs列表中出现的次数,而不是BMIs列表中有多少元素进入该函数后计算为True

这里有一个想法可以改变这一点:每当函数under对给定的bmi计算为True时,我们将1添加到另一个列表中,然后简单地询问列表的长度:

individuals_under = []
for bmi in BMIs:
    if under(bmi):
        people_under.append(1)
count1 = len(individuals_under)

另一个解决方案是使用列表理解并计算每个函数返回True的BMI数-这更简单,但您可能还不熟悉语法:

count1 = [under(bmi) for bmi in BMIs].count(True)

希望这足够清楚,以便您理解为什么它也发生在count2count3

就像@jfaccioni所说的

线路

count1 = BMIs.count(under)

正在计算函数under本身在BMIs列表中出现的次数,而不是BMIs列表中有多少个元素进入该函数后计算为True

然而,我对@jfaccioni有一种稍微不同的方法

    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 = []
for i in range(1):
    user = str(input("Please enter the name of one of the individuals: "))
    individuals.append(user)
    
BMIs = []
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)

Count = []
for i, bmi in enumerate(BMIs):
    if under(bmi):
        print (individuals[i], "is underweight")
        Count.append("under")
    elif normal(bmi):
        print (individuals[i], "is normal weight")
        Count.append("normal")
    elif over(bmi):
        print (individuals[i], "is overweight")
        Count.append("over")

count1 = Count.count("under")
count2 = Count.count("normal")
count3 = Count.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)

相关问题 更多 >

    热门问题