[Python] How do you figure out the item in a list and also the index of that item?

Discussão em 'Python' iniciado por Stack, Fevereiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a 5x5 playing area. Empty spaces are represented as 0 and the player as 8. I am trying to move the player based on input. I am having trouble knowing the value of an item inside a list while also knowing the index of that item. I tried enumerate() but I don't understand how it works:

    screenLine = "----------------------------------------------------------------";
    playingArea = ["", "8", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", ]

    def printPlayingArea():
    for i in range(len(playingArea)):
    if i == 5 or i == 10 or i == 15 or i == 20 or i == 25 or i == 30 or i == 35 or i == 40 or i == 45 or i == 50:
    print(playingArea)
    elif i == 0:
    print(playingArea, end="")
    else:
    print(playingArea, end=" ")
    print(screenLine)

    async def movePlayer():
    move = input("What direction would you like to go? ")
    print(screenLine)
    for a, b in enumerate(playingArea):
    if a == "8":
    print(a)
    print(b)
    movePlayer()


    Output:

    What direction would you like to go? right
    ----------------------------------------------------------------


    movePlayer() doesn't do anything and that is intentional. I am just trying to figure out the problem. The print statements are for testing and it printed nothing. IDE I am using is Programiz Pro Python Playground.

    Continue reading...

Compartilhe esta Página