Python Forum
For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game
#1
Hello,

I'm going through an introductory book on Python and there's a chapter in which the author presents code for a simple tic-tac-toe game (see below):

theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ', 'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ', 'low-L': ' ', 'low-M: ' ', 'low-R': ' '}

def printBoard(board):
     print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
     print('-+-+-')
     print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
     print('-+-+-')
     print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])
turn = 'X'
for i in range(9):
     printBoard(theBoard)
     print('Turn for ' + turn + '. Move on which space?')
     move = input()
     theBoard[move] = turn
     if turn == 'X':
          turn = '0'
     else:
          turn = 'X'
printBoard(theBoard)
I think I understand everything that is going on in this code except for lines 10 and 14:

- Line 10: I understand that there needs to be some sort of loop in order for the game to work, but I'm not sure what the for loop here is doing. Range(9) in Python generates 10 numbers, correct: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9? If that's right, why is the main code in the program (which is indented under the for loop) set to run 10 times? I thought this might have something to do with the layout of the tac-tac-toe board, but that only has nine (not 10) slots.

- Line 14: I understand that the game takes the user's move as input and then needs to apply that move to the existing tic-tac-toe board somehow. I also understand somewhat intuitively how lines 15-18 alternate the turns between the two players. But I don't think I understand the actual mechanics of how the board (which is a dictionary) is being modified by the use of the brackets in "theBoard[move] = turn."

Thank you so much in advance for any help.
Reply
#2
Line 10: Python starts from number 0, so its 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 which results in 10 numbers.
Reply
#3
ON line 14:
theboard[]
being a dictionary, with available slots mid-r etc etc, we are defining which player, x or o, is in that slot.
theBoard[move] = turn
, which defines say
theBoard['top-L'] = 'x'
,

then we printboard and show the board with the updated values.
Reply
#4
Does this all clear up your misunderstandings?
Reply
#5
Range generates numbers up to but not including the end, so range(9) generates numbers 0, 1, 2, 3, 4, 5, 6, 7, 8.
Reply
#6
Thanks Linenloid and deanhystad. So in technical terms, what is going on the left-hand side of line 14 (left of the equals sign)? I know "theBoard" has been defined as a dictionary, "move" is a variable has been defined as user input, and "turn" has been defined as the string "X" (at least initially). But how would you describe what is happening in "theBoard[move]"? Would you say that this is some kind of assignment statement? Or something else?

Thanks!
Reply
#7
Where is the "="? If there is no "=" there is no assignment. Not completely true, but mostly true.

theBoard[move] is a dictionary lookup. It will return the value associated with the key referenced by the variable "move". If move == 'top-R', theBoard[move] will return the value in the dictionary associated with the key 'top-R'. If theBoard does not contain the key 'top-R' this code raises an error.
Reply
#8
Thanks again deanhystad, that's very helpful.

I think I understand what is going on in the for loop now but just want to confirm: Is it correct that the for loop "remembers" the most recent value of "turn" (either X or 0) when it's done executing the "if" or "else" statement at the bottom and goes back up to the top again? For example:

1 ) On the first turn, X moves into the space top-L
2 ) Python sets the value for top-L as X (given that turn has been defined as X on line 9)
3 ) Python enters the if statement and changes turn to 0. When the code re-runs on the next iteration, the print statement is changed to 0 and the value for the user's input is applied as 0 in "theBoard[move] = turn" (since turn has been redefined as 0)
4 ) Python enters the else statement and changes turn to X. The code then re-runs on the next iteration and the process repeats (until a total of 10 iterations have been run)

Do I have it right?

Thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 1,508 Sep-03-2022, 11:21 PM
Last Post: Winfried
  While loop not ending (Best of 10 dice game) K3nidi 3 1,485 Jul-09-2022, 09:53 AM
Last Post: K3nidi
  Reading list items without brackets and quotes jesse68 6 4,613 Jan-14-2022, 07:07 PM
Last Post: jesse68
  Data pulled from SQL comes in brackets nickzsche 3 2,643 Jan-04-2022, 03:39 PM
Last Post: ibreeden
  loop adventure game ilikedofs 1 1,699 May-26-2021, 12:43 AM
Last Post: bowlofred
  Getting a certain value from inside brackets. LeoT 5 2,995 Mar-01-2021, 03:34 PM
Last Post: buran
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,691 Jan-21-2021, 02:21 PM
Last Post: InputOutput007
  Iterating over a dictionary in a for loop - checking code has worked sallyjc81 1 1,919 Dec-29-2020, 05:14 PM
Last Post: ndc85430
  How to modify item in dictionary? Winfried 7 3,451 Nov-21-2020, 07:12 PM
Last Post: bowlofred
  Designing a "game" loop api pitosalas 2 2,470 Jun-07-2020, 03:20 PM
Last Post: pitosalas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020