You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python团队信息录入程序求助:实现列表嵌套存储团队与成员

团队及成员信息收集程序解决方案

看起来你已经有了一个不错的开头!要实现团队名称和嵌套成员信息的存储,我们可以用字典来表示每个团队(包含名称和成员列表),然后把这些字典存入Teams列表中,这样就能完美实现你要的嵌套结构啦。

下面是完整的代码实现,我帮你补全了缺失的部分,还加了一些实用细节:

# Creating Teams
print("Welcome to the program")
print("======================================================")
Teams = []

# Looping to collect 4 different teams and their players
for x in range(0, 4):
    # Ask for the team name
    team_name = input(f"What is the name of team {x+1}? ")
    
    # Collect team members
    team_members = []
    print(f"Enter members for {team_name} (type 'done' when finished):")
    while True:
        member = input("- ")
        if member.lower() == 'done':
            break
        # Skip empty inputs to avoid invalid entries
        if member.strip():
            team_members.append(member.strip())
    
    # Create a dictionary for the team and add to the Teams list
    team = {
        "name": team_name,
        "members": team_members
    }
    Teams.append(team)

# Optional: Display collected data to verify correctness
print("\n--- Collected Team Information ---")
for idx, team in enumerate(Teams, 1):
    print(f"\nTeam {idx}: {team['name']}")
    print("Members:")
    for member in team['members']:
        print(f"- {member}")

关键细节说明:

  • 每个团队用字典存储,包含name(团队名)和members(成员列表)两个键,成员信息自然嵌套在对应团队结构下。
  • 用while True循环收集成员,直到用户输入done结束,比固定成员数量更灵活。
  • 加入空输入过滤,避免存入无效的成员名称。
  • 最后添加了数据展示部分,方便你快速确认收集的信息是否正确。

如果之后需要调整团队数量,只需要修改range(0,4)里的数字就行——比如改成range(0,5)就能收集5个团队的信息。

内容的提问来源于stack exchange,提问作者Flyy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:30:28