Reference to the same row


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/0/3?curriculum_id=4f89dab3d788890003000096


Oops, try again. It looks like your board has 5 references to the same row! This will cause problems later on. Read the hint for more information and use a for loop to complete this lesson


I expect the code to run with out errors
What is the reference error and waht problems would be caused later?

Replace this line with your code. 
board = []

row = ['O']*5

for i in range(5):
    i = row
    print i
    board.append(i)
print board


#2

well, row is a list

here:

i = row

this will not create a copy of a list, this varaible will just reference to the row list

so you are appending the same list 5 times to board

which means, that if we where to update a missed guess with X, this would happen in every row instead of just one row


#3

ok. Thanks for the reply.
But is there any other way in which the variable i creates a new copy of the list instead of referring to it.


#4

i would do what the exercise recommends, simply append ['O']*5 to board, this will create a new list every time


#5

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