尝试使用Python中的值命名列表

2024-04-20 14:28:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个系统,在这个系统中,用户可以在游戏中从1到3选择要面对多少个机器人,但我很难弄清楚如何使用给定的值作为名称自动创建机器人。你知道吗

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")

这部分工作正常,但我不知道如何使用Player_Count变量作为列表的名称(bot的数据将存储在这些列表中)

我做过,但我想这是一个非常糟糕的方法:

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

任何帮助都将不胜感激!你知道吗


Tags: 用户名称true游戏列表if系统bot
1条回答
网友
1楼 · 发布于 2024-04-20 14:28:38

制作一个列表,每个机器人都有自己的内部列表,机器人的总数由输入的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)]

您可以通过len(bots)找到bot的数量,并索引到bots以找到每个单独的框。你知道吗

相关问题 更多 >