Bar Shahar
Bar Shahar

Reputation: 11

Trying to name a list using a value in Python

I'm trying to make a system where a user chooses how many bots he wants to face against from 1 to 3 in a game but I'm having a hard time figuring out how to automatically create the bots using the given value as a name.

while True:
    try:
        Player_Count = int(input("How many bots do you want to play against from 1 - 3?"))
    except ValueError:
        print("That's not a number! Try again!")
    else:
        if 1 <= Player_Count <= 3:
            break
        else:
            print("Out of Range! Try Again")

That part works fine, but I can't figure out how to use the Player_Count variable as a name for the lists (The data for the bots will be stored in those lists.)

I've did this but it's a pretty bad way to do that, I suppose:

for bot in range(Player_Count):
    if bot == 0:
        bot1 = []
    if bot == 1:
        bot2 = []
    if bot == 2:
        bot3 = []

Any help will be greatly appreciated!

Upvotes: 0

Views: 54

Answers (1)

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

Make a list of lists, where each bot has its own inner list, and the total number of bots is determined by the inputted player_count:

player_count = int(input("How many bots do you want to play against from 1 - 3?"))
bots = [list() for _ in range(player_count)]

You can find the number of bots by len(bots), and index into bots to find each individual box.

Upvotes: 3

Related Questions