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


Messages In This Thread
For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game - by new_coder_231013 - Dec-26-2021, 12:39 PM

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