如何在每次迭代中将循环中要求的输入数据存储到单独的列表中?

how can I store inputted data asked for in a loop to a separate list each iteration?

提问人:alex 提问时间:9/25/2023 更新时间:9/25/2023 访问量:44

问:

我正在编写一个程序,要求用户提供 2 到 10 个之间的球队数量,然后获取球队名称和该球队的球员姓名,直到用户输入完成。然后,它会移动到下一个球队名称和球员。最后,一旦为最终球队输入完成,它就会打印每个球队的名称和每支球队的球员姓名。问题是我无法弄清楚如何在循环继续时存储球队名称和球员名称。



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 循环会更有效吗?我试图为每个团队创建一个列表,但当程序再次循环时,它也被覆盖了。

我的目标输出将显示为

团队 #1:钢人

玩家 #1:鲍勃

玩家 #2:乔

团队#2:海豚

球员 #1:蒂米

玩家 #2:伊莱

等等

python 循环 输入

评论


答:

2赞 Jesse Sealand 9/25/2023 #1

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

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

这将生成具有以下数据结构的字典,如上所述

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
0赞 Reilas 9/25/2023 #2

"...我不知道如何在循环继续时存储球队名称和球员姓名。..."

for 循环之前声明一个空集合

teams = {}
while numteams := int(input("How many teams are there? ")):
    if not 2 <= numteams <= 10:
        print("Please enter between 2 and 10 teams.")
    else: break
#Ensures the proper number of teams are entered.
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.")
for i in range(numteams):
    k = input(f'Team #{i + 1}: ')
    v = []
    j = 0
    while (p := input(f'Player #{(j := j + 1)}: ')) != "done":
        v.append(p)
    teams[k] = v

输出

How many teams are there? 2
Please enter a team name when prompted. Then enter player names for that team. 
When done entering player names for that team, type done.
Team #1: steelers
Player #1: bob
Player #2: joe
Player #3: done
Team #2: dolphins
Player #1: timmy
Player #2: eli
Player #3: done
{'steelers': ['bob', 'joe'], 'dolphins': ['timmy', 'eli']}