[3. Make a List] have qeustion


for i in range(5):

↑ this way I prespect that

0 [0][0][0][0][0]
1 [0][0][0][0][0]
2 [0][0][0][0][0]
3 [0][0][0][0][0]
4 [0][0][0][0][0]

but infact


I don’t know why


But it passed, right?


@teraace33534: That’s because you’re printing the entire list other than each part of it.
If you want to print each element, not everything, you should print each position, just like you’d print a list containing values from 0 to 5.

Try doing this:

board = [] for i in range(5): print board[i]
You’ll see you’ll get the output you want :slightly_smiling:

for i in board: print i
Works the same way! But the first example will access each position of your list and print the values.


It’s explained in exercise 5!


It didn’t work for me. The error said: Traceback (most recent call last):
File “python”, line 3, in
IndexError: list index out of range


@wearyboy: You sure that didn’t work? The range function is supposed to generate numbers from 0 to n-1 so doing this wouldn’t be wrong at all.


Hello! I understand why the following code works:

   board = [] 
   for i in range(0,5):
        board.append(["O"] * 5)
    print board

but is there a way to write this code using the second range(len()) method described in previous lessons? I’ve tried:

  board = []   
  for i in range(len(4)):
       board.append(["O"] * 5)
    print board[i]

but get the following error:
Your code threw the following error: object of type ‘int’ has no len()



an integer does not have length
len is for lists or string
If you want the len of an integer, you have to convert first the integer to str


try what worked for me

board = [] for i in range(0, 5): p = ["O", "O", "O", "O", "O"] board.append(p)


Tks very much, i passed it