[Python] IndexError: list index out of range - for i in range(len()): not working [closed]

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    Version 1

    farm = [["spinach", 5],["strawberry", 4]]
    for i in range(len(farm)):
    print([0], " :", [1], "days left")


    Version 2

    farm = [["spinach", 5],["strawberry", 4]]
    for i in range(len(farm)):
    if i > len(farm):
    print("Stop")
    else:
    print([0], " :", [1], "days left")


    I'm quite new to python, so sorry if this is an obvious question. But both of these give the same error (IndexError: list index out of range) for the 3rd line, and I can't quite figure out why. Version 2 was me attempting to fix it, but it still gives the same error. Any help is appreciated!

    Continue reading...

Compartilhe esta Página