我正在编写一个程序,要求用户输入2到10支球队的数量,然后获取球队名称和球队中球员的名称,直到用户输入Done.然后,它转到下一个球队名称和球员.最后,一旦输入最终球队的名称,它就会打印每支球队的名称和每支球队的球员的名字.问题是,当循环继续时,我不知道如何存储球队的名字和球员的名字.



numteams = int(input("How many teams are there? "))
if numteams < 2 or numteams > 10:
    print("Please enter between 2 and 10 teams.")
#Ensures the proper number of teams are entered.
else:
    print("Please enter a team name when prompted. Then enter player names for that team. \nWhen done entering player names for that team, type done.")
    z = 1
    while z <= numteams:
    #overall loop to inquire about all teams
        teamname = input("Please enter the name of Team #" + str(z) + ": ")
        name = " "
        while name != "done":
        #loop to ask for names of players 
            name = input("Please enter the name of a player for team " + teamname + ": ")
        z = z + 1
    

这是我当前的代码,它成功地根据有多少支球队询问了球队的名称和球员的名字,但我不知道如何打印每支球队及其球员,因 for each 输入变量在循环的每次迭代中都会被覆盖.取而代之的是for循环会不会更有效?我试图 for each 团队创建一个列表,但当程序再次循环时,它也被覆盖了.

我的目标输出将显示为

第一队:钢人队

第一号玩家:鲍勃

2号玩家:乔

第二组:海豚

第一号玩家:蒂米

玩家2:

诸若此类

推荐答案

只需对原始代码进行最小数量的更改,我们就可以添加一个字典,将球队名称存储为关键字,并将球员名称存储在列表中作为其值.

team_dict = {}

numteams = int(input("How many teams are there? "))
if numteams < 2 or numteams > 10:
    print("Please enter between 2 and 10 teams.")
    
#Ensures the proper number of teams are entered.
else:
    print("Please enter a team name when prompted. Then enter player names for that team. \nWhen done entering player names for that team, type done.")
    z = 1
    
    while z <= numteams:
        
        #overall loop to inquire about all teams
        teamname = input("Please enter the name of Team #" + str(z) + ": ")
        
        player_names = []
        player_name = ""
        
        #loop to ask for names of players
        while player_name != "done":
            
            player_name = input("Please enter the name of a player for team " + teamname + ": ")
            player_names.append(player_name)
            
        team_dict[teamname] = player_names
        
        z = z + 1

这将生成如下数据 struct 的词典,如上所述

print(team_dict)

{'Yankees': ['Me', 'Myself', 'I', 'done'],
 'Pirates': ['Me again', 'Myself again', 'I again', 'done']}

然后,我们可以迭代每个条目的球队和球员名称,并控制打印输出的方式.例如

for key,val in team_dict.items():
    
    print(f"Team: {key}")
    
    for player in val:
        print(f"    Player: {player}")


# Team: Yankees
#     Player: Me
#     Player: Myself
#     Player: I
#     Player: done
# Team: Pirates
#     Player: Me again
#     Player: Myself again
#     Player: I again
#     Player: done

Python相关问答推荐

如何在Pandas 中存储二进制数?

手动为pandas中的列上色

情节生成的饼图文本超出页面边界

socket.gaierror:[Errno -2]名称或服务未知|Firebase x Raspberry Pi

Tkinter -控制调色板的位置

如何在超时的情况下同步运行Matplolib服务器端?该过程随机挂起

给定数据点,制定它们的关系

过滤绕轴旋转的螺旋桨

在使用Guouti包的Python中运行MPP模型时内存不足

如何让 turtle 通过点击和拖动来绘制?

如果条件为真,则Groupby.mean()

Pandas 滚动最接近的价值

在Pandas DataFrame操作中用链接替换'方法的更有效方法

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

使用setuptools pyproject.toml和自定义目录树构建PyPi包

连接一个rabrame和另一个1d rabrame不是问题,但当使用[...]'运算符会产生不同的结果

名为__main__. py的Python模块在导入时不运行'

处理具有多个独立头的CSV文件

处理Gekko的非最优解

从一个df列提取单词,分配给另一个列