Make a List: Need Assistance Interpreting the Code


#1




I was able to correct my mistakes be referencing previous posts but wanted to understand the flawed logic in my original response. I don't believe I fully understand the use of 'range()'. Can someone please help me to understand why the range '5' without referencing the variable 'board' in the looping command works. Thanks for your assistance!



#2

The range is a list from lowerbound included to upperbound, excluded. It is the iterable in the loop.


#3

Thank you for your assistance! One more question; why is the correct code not 'for i in board(5):' ... I suppose I am curious as to how the program understands we are referencing our empty 'board' list without directly referencing the name.


#4

board is already a list, with a fixed (declared) length. It is an iterable.

for i in board:

range() is a function that returns a list. That's why we need to pass parameters that define the iterable it generates.


#5

Oh, I see what you mean. Thanks for the help!


#6

Thanks for you explanation, it helped me too.:sunglasses:


#7

The explanation really helped. Just to further clarify my understanding, it is the board.append(["O"] * 5) that causes O to repeat 5 times in 1 list and range(0,5) or range(5) that causes this list of O repeating 5 times to repeat 5 times, right? Sorry for the confusing wording.


#8

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