How can I get dictionary key as variable directly in Python (not by searching from value)?

前端 未结 14 1796
面向向阳花
面向向阳花 2020-12-04 06:19

Sorry for this basic question but my searches on this are not turning up anything other than how to get a dictionary\'s key based on its value which I would prefer not to us

14条回答
  •  误落风尘
    2020-12-04 07:05

    This code below is to create map to manager players point. The goal is to concatenate the word "player" with a sequential number.

    players_numbers = int(input('How many girls will play? ')) #First - input receive a input about how many people will play
    
    players = {}  
    counter = 1
    
    for _ in range(players_numbers): #sum one, for the loop reach the correct number
        player_dict = {f'player{counter}': 0} #concatenate the word player with the player number. the initial point is 0.
        players.update(player_dict) #update the dictionary with every player
        counter = counter + 1
        print(players)
    

    Output >>> {'player1': 0, 'player2': 0, 'player3': 0}...

提交回复
热议问题