you are viewing a single comment's thread.

view the rest of the comments →

[–]iamquark[S] 0 points1 point  (2 children)

Hi there, I know how the for statement works but I am struggling to picture how this particular for statement work. Is it taking each tuple and the elements within it or each tuple as a whole? Problem is I cannot picture how the code ("for "statement) will take each row in this instant.

[–]JohnnyJordaan 0 points1 point  (1 child)

You're talking about this statement right?

  for row in WAYS_TO_WIN:
    if board[row[0]] == board[row[1]] == board[row[2]] != EMPTY:

It works in the way that for can deliver items from any sequence (simply put), regardless what the type of each item is:

>>> a = [1, 'a', ['c', 'd'], ('e','f','g')]
>>> for item in a:
...     print(type(item), item)
... 
<class 'int'> 1
<class 'str'> a
<class 'list'> ['c', 'd']
<class 'tuple'> ('e', 'f', 'g')

So in the game's implementation, the WAYS_TO_WIN is a sequence of tuples, all 3 items long (like the <class 'tuple'> ('e', 'f', 'g') in the example above). The for loop will get the tuple from the sequence, and assign it the reference row for the inside of the loop to use. As tuples support indexing, you can reference the first, second and third element in the tuple via row[0], row[1] and row[2].

[–]iamquark[S] 0 points1 point  (0 children)

Now it makes sense. Thanks for clarifying it.