在附加列表(用户输入)(Python)中计数和分离年龄

发布于 2025-02-12 14:38:36 字数 611 浏览 0 评论 0 原文

我自己试图自己弄清楚这一点,但我无法获得想要的结果,而且我无法通过互联网找到正确的答案。我有一块代码,要求用户10年龄并将其存储在附录列表中,问题是我可以按年龄分开它们,但我不知道如何按年龄组来计数数量。我尝试寻找文档,但其中大多数是带有已知值列表的示例。谁能给我一些建议? 到目前为止,这是我的代码:

age = []
for i in range (10):
    ag = int (input("Write your age"))
    age.append(ag)
print ("The number of kids are: ")
for i in range (10):
    if age [i] < 12:
        print (age [i])
print ("The number of teenagers are: ")
for i in range (10):
    if age [i] >=12 and age [i] < 17:
        print (age [i])
print ("The number of adults are: ")
for i in range (10):
    if age [i] >= 18:
        print (age [i])

I've trying to figure out this myself but I cannot get the results I want, and I've been unable to find the right answer looking through the internet. I have a piece of code that ask the user 10 ages and stores them in an append list, the problem is I can separate them by ages but I don't know how to count the amount by age group. I tried looking for documentation but most of it are examples with a list with known values. Can anyone give me some advice?
This is my code so far:

age = []
for i in range (10):
    ag = int (input("Write your age"))
    age.append(ag)
print ("The number of kids are: ")
for i in range (10):
    if age [i] < 12:
        print (age [i])
print ("The number of teenagers are: ")
for i in range (10):
    if age [i] >=12 and age [i] < 17:
        print (age [i])
print ("The number of adults are: ")
for i in range (10):
    if age [i] >= 18:
        print (age [i])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅一夜美梦 2025-02-19 14:38:36

仅使用一个循环,一个构建数据的循环,同时为每个类别增量计数器

ages = []
kids, teen, adult = 0, 0, 0

for i in range(10):
    age = int(input("Write your age"))
    ages.append(age)

    if age < 12:
        kids += 1
    elif age <= 17:
        teen += 1
    else:
        adult += 1

print("The number of kids is: ", kids)
print("The number of teenagers is: ", teen)
print("The number of adults is: ", adult)

Use only one loop, the one that builds the data, at the same time increment counter for each category

ages = []
kids, teen, adult = 0, 0, 0

for i in range(10):
    age = int(input("Write your age"))
    ages.append(age)

    if age < 12:
        kids += 1
    elif age <= 17:
        teen += 1
    else:
        adult += 1

print("The number of kids is: ", kids)
print("The number of teenagers is: ", teen)
print("The number of adults is: ", adult)
百思不得你姐 2025-02-19 14:38:36

一旦收集了年龄,就可以将它们过滤为子列表,类似:

ages = [5, 10, 17, 18, 20, 30, 50]

kids = [a for a in ages if a <= 12]
teens = [a for a in ages if a > 12 and a < 20]
adults = a for a in ages if a >= 20]

# print summaries
print(f'there are {len(kids)} kids: {kids}')
print(f'there are {len(teens)} teens: {teens}')

上面的示例是使用Python的功能,称为

您也可以明确地循环到项目上,并在需要时打印/填充值:

kids = []
teens = []
...
for age in ages:
    if age < 13:
        kids.append(age)
    elif age < 20:
        teens.append(age)
    else:
        ...
print('teens are', teens)

once you've collected your ages, you could filter them into sub-lists, something like:

ages = [5, 10, 17, 18, 20, 30, 50]

kids = [a for a in ages if a <= 12]
teens = [a for a in ages if a > 12 and a < 20]
adults = a for a in ages if a >= 20]

# print summaries
print(f'there are {len(kids)} kids: {kids}')
print(f'there are {len(teens)} teens: {teens}')

The above example is using a feature of python called list comprehensions [value for value in list (if condition)]

You could also loop over the items explicitly and print/populate values if you wanted:

kids = []
teens = []
...
for age in ages:
    if age < 13:
        kids.append(age)
    elif age < 20:
        teens.append(age)
    else:
        ...
print('teens are', teens)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文