0

The example is below. I want to add n player numbers and replace the player1 for player# where # is a variable counter using something like addend on a list.

this is the code

from random import randint
game={'player1':randint(1,6),
      'player2':randint(1,6),
      'player3':randint(1,6)}

The print is: {'player1': 2, 'player2': 2, 'player3': 5}

I want something like:

from random import randint
for c in range(1,8)
game={f'player{c}':randint(1,6)}

The print is: {'player3': 5}

  • The first code block does not result in the print you're claiminig, and I strongly doubt that second codeblock prints anything other than `SyntaxError: invalid syntax`, so please [post your real code](/help/how-to-ask), never just write code in your post without actually verifying that what you wrote does what you claim it does. – Mike 'Pomax' Kamermans Jan 31 '23 at 19:38
  • 1
    use dictionary comprehension: `game = {f'player{c}': randint(1,6) for c in range(1,8)}` – It_is_Chris Jan 31 '23 at 19:39

0 Answers0