As part of the project, I am supposed to build a memory game, which involves randomizing a list of 16 digits (two identical ranges of range(7)) and printing those digits onto a canvas. My first iteration had a for loop that picks each number of the list and prints it onto the canvas at a set interval (middle_cell_point) as such:
def draw(canvas):
middle_counter = 1
for num in full_list:
canvas.draw_text(str(full_list[num]), (((middle_cell_point * middle_counter))
- (middle_cell_point/2) , HEIGHT/2), 22, "Red")
middle_counter += 1
the output from this repeats numbers on the canvas, but when I debug the code it seems to accurately assign numbers and does not seem to repeat any. The program works properly When I replaced "full_list" with "range(len(full_list))" in the for-loop:
def draw(canvas):
middle_counter = 1
for num in range(len(full_list)):
canvas.draw_text(str(full_list[num]), (((middle_cell_point * middle_counter))
- (middle_cell_point/2) , HEIGHT/2), 22, "Red")
middle_counter += 1
could someone explain to me why the second iteration works and the first one does not?
Thanks a lot!
The difference is that
for num in full_listlists the items andfor num in range(len(full_list))lists the indices.To illustrate:
In your case you want to use
numinstead offull_list[num]: