0
我需要爲不同部門的替代席位分配座位,但是當我嘗試去做時,我會收到錯誤。Python索引錯誤超出範圍
下面我粘貼代碼:
#matrix for dept1
X = [111, 222, 333, 444, 555, 666, 777 ,888, 999, 123, 234,345]
#matrix for dept2
Y = [1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999,9876, 8765,7654]
#empty class room which consist of 18 students
class_room = [[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]]
ca = 0
pa = 0
print 'combining dept1 and dept2 in alternative manner'
for i in range(5):
for j in range(25):
if j%2 == 0:
class_room[i][j] = X[pa]
pa += 1
else:
class_room[i][j] = Y[ca]
ca += 1
print class_room
#error - Traceback (most recent call last):
# File "C:\Users\SONY\workspace\mine\taest3.py", line 24, in <module>
# class_room[i][j] = X[pa][pa]
#IndexError: list index out of range
好心幫我整頓和明確的。
非常感謝你@Arya McCarthy –
樂意幫忙。如果這特別有用,我鼓勵你加入它。 –