Make a List


#1

(My code)
board = []

lists = [“o”,“o”,“o”,“o”,“o”,]
for i in range(0,len(lists)):
board.append(lists)

print board

(Correct code)
board = []
for i in range(5):
board.append([‘O’] * 5)

print board

(Instructions)
Create a 5 x 5 grid initialized to all 'O’s and store it in board.

============================================================
What is wrong with my own code above? The result seems to be the same…I looped “lists” 5 times, which is the number of its length. What should I have done to make it work? Any other way to do the same without using the correct code?


#2
board = [][]
for i in range(5):
  for j in range(5):
    board[i][j]='O'

Defines board as a 2D list, loops through numbers 1 to 5 on each (i represents row, j represents column in each iteration), and fills each position with ‘O’.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.