Python list IndexError (easy)

Asked 2 years ago, Updated 2 years ago, 15 views

ho =[]

for i in range(1,5) :

    for j in range(1,4) :

        ho[i][j] = (i*100 + j)

for i in range(1,5) :

    for j in range(1,4) :

        if ho[1][1] or ho[3][1] or ho[2][3] or ho[4][4] :

            continue

        else :

            print("bottled water delivery: %d", ho[i][j])

It's a simple two-dimensional list problem. We deliver water to the villa, except for the houses written on the if, such as 101 and 301 Print it out to show delivery completion. I keep getting errors

 ho[i][j] = (i*100 + j)
IndexError: list index out of range

This is an error. But it's simple to put a value in the list index I don't know why this error occurs.

Here's the code I changed after looking at the two-dimensional list in the book. Similarly, there is an error.

ho = []
ho2 = []

for i in range(1,5) :
    for j in range(1,4) :
        ho[j].append = (i*100 + j)
    ho[i] .append(ho[j])
    ho[j] = []

for i in range(1,5) :
    for j in range(1,4) :
        if ho[1][1] or ho[3][1] or ho[2][3] or ho[4][4] :
            continue
        else :
            print("bottled water delivery: %d", ho[i][j])
    ho[j].append = (i*100 + j)
IndexError: list index out of range

What's the reason?

python

2022-09-22 19:12

1 Answers

ho = []

Not

ho = [[0]*5 for i in range(5)]

If you change it to and proceed, there will be no index error.


2022-09-22 19:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.