1

For a monopoly like game I am making I have a section that asks the user the name of the different players.

The number of times, this is asked will be based off of a previous input which asks the user how many players are there.

And from that, there would be different variables set for the number of player.

IE, if the number of players stated in the input was 4, then there would be variables for player 1 with their inputted name and so on to player4.

How would I create a number of variables based on the number of players inputted and also ask the names of each player?

The variables would look somewhat like this:

player1 = John
player2 = Doe
player3 = Sam
tomerpacific
  • 4,704
  • 13
  • 34
  • 52
warherolion
  • 115
  • 5
  • 2
    Possible duplicate of [How do you create different variable names while in a loop?](https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop) – Alec Apr 21 '19 at 05:25

3 Answers3

1

You probably want a single variable. This could either be a list or a dict. In either case, you'd probably want a dict inside either of these to represent information about each player. So the list you would build up would look like this:

players = [
    {
        "name": "Joe",
        "money": 1234,
        "properties": [
            "Boardwalk",
        ]
        "railroads": [
            "Reading",
        ],
        ...
    },
    {
        "name": "Sam",
        "money": 4444,
        "get_out_of_jail_card": true,
        ...
    }
}

You could build up this list as you take information about each player. Start with an empty list, and then add players:

startingMoneyValue = 20000

...

player_count = int(input("Enter number of players: "))

players = []

for i in range(player_count):
    playerName = input("What is the next player's name?")
    players.append({
        "name": playerName,
        "money": startingMoneyValue,
        "properties": [],
        "railroads": [],
        ...
    })

The key here is that once you've created each player's record, you can get at and modify that data easily. For example, to get the amount of money the second player has, you would do this:

money = players[1]['money']

and when player #3 passes go:

players[2]['money'] += 200

You can initialize all of the particular values you associate with each player up front, or you could add some of the values later, as things happen.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
0

You’ll want to loop through a range based on the number of players. I don’t think you can generate variables in a script like you are asking. But a list works just as good.

number_of_players = input("Enter number of players: ")
players = []
for player in range(number_of_players):
    players.append(input(f'Enter player {player+1} name: '))
Daniel Butler
  • 3,239
  • 2
  • 24
  • 37
0

You can use a dictionary:

playerdict, players = {}, int(input('How many players are there?'))

for x in range(players):
    playerdict[f'player{x+1}'] = input(f'What is player {x+1}\'s name?')
Alec
  • 8,529
  • 8
  • 37
  • 63