[3. Make a List] have qeustion


#1

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

↑ 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

[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]

I don't know why


#2

But it passed, right?


#3

@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.


#4

It's explained in exercise 5!


#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


#6

@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.


#7

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()

Thanks!


#8

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


#9

try what worked for me

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


#10

Tks very much, i passed it