0

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!

2
  • 2
    Why don't you use python dictionary, where bot is key and list is value and then as bot and list are mapped you can access them easily. Commented Dec 28, 2019 at 4:19
  • Don't use dynamic variables, use a container like a list or a dict Commented Dec 28, 2019 at 6:02

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.