modifying an element in a 2D array for discord.py

126 views Asked by At

I'm making a discord bot so that I can make tournaments for League 5v5. Here is the command I made for making the teams:

rows, cols = 5, 2
teamArr = [["Empty" for x in range(rows)] for y in range(cols)]
teamNum = ""
playerName = ""
lane = ""

@bot.command()
async def team(context, name, num, pos): #member: discord.Member
  teamEmbed = discord.Embed(title="Teams", color=0x6a0dad)
  playerName = str(name)
  teamNum = str(num)
  lane = str(pos)

  #team1
  if (teamNum == 1):
    if (lane == 'Top'):
      teamArr[0][0] = str(playerName)
    elif (lane == 'Jungle'):
      teamArr[0][1] = str(playerName)
    elif (lane == 'Mid'):
      teamArr[0][2] = str(playerName)
    elif (lane == 'Adc'):
      teamArr[0][3] = str(playerName)
    elif (lane == 'Support'):
      teamArr[0][4] = str(playerName)

  #team2
  elif (teamNum == 2):
    if (lane == 'Top'):
      teamArr[1][0] = str(playerName)
    elif (lane == 'Jungle'):
      teamArr[1][1] = str(playerName)
    elif (lane == 'Mid'):
      teamArr[1][2] = str(playerName)
    elif (lane == 'Adc'):
      teamArr[1][3] = str(playerName)
    elif (lane == 'Support'):
      teamArr[1][4] = str(playerName)

  teamResult = str(teamArr[0][0]) + "\n" + str(teamArr[0][1]) + "\n" + str(teamArr[0][2]) + "\n" + str(teamArr[0][3]) + "\n" + str(teamArr[0][4])
  teamEmbed.add_field(name="Team 1", value=teamResult, inline=True)
  teamResult2 = str(teamArr[1][0]) + "\n" + str(teamArr[1][1]) + "\n" + str(teamArr[1][2]) + "\n" + str(teamArr[1][3]) + "\n" + str(teamArr[1][4])
  teamEmbed.add_field(name="Team 2", value=teamResult2, inline=True)

  await context.send(embed=teamEmbed)

I made the teamArr with all the elements to be "Empty." I am setting teamArr[0][0] = playerName so that I am changing the string "Empty" to the player's name. However, whenever I print it out, it just stays as "Empty" and does not seem to work. What would I be possibly doing wrong here?

1

There are 1 answers

1
Eric Jin On

You are setting teamNum = str(num) but then comparing teamNum == 1 and teamNum == 2. Therefore, neither comparison is correct, and your code skips to the end. You can fix this by doing something such as int(teamNum) == 1 or teamNum == '1'.

Sidenotes:

  1. You should use converters to save yourself the hassle of converting yourself: async def team(ctx, name: str, num: int, lane: str). There's also no need to str() everything multiple times.
  2. You can make printing out the state easier by doing '\n'.join(teamArr[0]).
  3. For better style, you can use teamArr[teamNum-1] instead of writing it twice.