talkpython / python-for-absolute-beginners-course

Code samples and other handouts for our course.
https://training.talkpython.fm/courses/explore_beginners/python-for-absolute-beginners
MIT License
2.3k stars 1k forks source link

Tic Tac Toe #4

Closed GeekDGirl closed 4 years ago

GeekDGirl commented 4 years ago

I'm working through the lecture on building a Tic Tac Toe game. I have followed your suggestion of putting 3 None data types into my lists to build the board. Unfortunately, my program won't run because I keep the following errors: //PycharmProjects/tictactoe/game.py:21: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma? [None, None, None] Traceback (most recent call last): File "//PycharmProjects/tictactoe/game.py", line 77, in main() File "//PycharmProjects/tictactoe/game.py", line 21, in main [None, None, None] TypeError: list indices must be integers or slices, not tuple

Is there some way I can change the program to get it to run? I have been following the steps you've taken exactly but don't achieve the same results. Any direction you can provide would be helpful. Thank you.

mikeckennedy commented 4 years ago

I think you are doing this:

cell = board[1,2]

rather than this:

cell = board[1][2]

But I can't be certain from the error. :)

GeekDGirl commented 4 years ago

def main():

Board is a list of rows

#Rows are a list of cells
board = [
    [None, None, None]
    [None, None, None]
    [None, None, None]
]

# CHOOSE INITIAL PLAYER
active_player_index = 0
players = ["You", "Computer"]
symbols = ["X", "O"]

# UNTIL SOMEONE WINS
while not find_winner(board):
    #SHOW THE BOARD
    player = players[active_player_index]
    symbol = symbols[active_player_index]

    announce_turn(player)
    show_board(board)
    if not choose_location(board, symbol):
        print("That isn't a option, try again.")
        continue

def choose_location(board, symbol): row = int(input("Choose which row: ")) column = int(input("Choose which column: "))

row -= 1
column -= 1
if row <0 or row >= len(board):
    return False
if column < 0 or column >= len(board[0]):
    return False

cell = board[row][column]
if cell is not None:
    return False

board[row][column] = symbol
return True

def showboard(board): for row in board: print("| ", end='') for cell in row: symbol = cell if cell is not None else "" print(symbol, end=" | ") print() def announce_turn(player): print() print(f"It's {player}'s turn. Here's the board") print()

def find_winner(board): return False

if name == 'main': main()

mikeckennedy commented 4 years ago

Thanks. Can you tell me which line above the error is on?

GeekDGirl commented 4 years ago

The error on line 21 refers to the [None, None, None] (first line in the board).

GeekDGirl commented 4 years ago

In case it makes a difference, I'm running Python 3.8.2.

grze744 commented 4 years ago

Hi! Looks like you don't have comas between entries in the primary list in the board, so the program sees the lower lists as tuples and can't reach individual cells of the board. Try this instead:

board = [ [None, None, None], [None, None, None], [None, None, None], ]

I hope that works! I constantly have similar problems with the interpunction, those can be tricky to find sometimes. I'm not used to such precision to be honest...

mikeckennedy commented 4 years ago

Hi @GeekDGirl and @grze744

@grze744 Thank you for pointing this out. Yes, that's exactly the problem:

board = [
[None, None, None] # <-- missing ,
[None, None, None] # <-- missing ,
[None, None, None] # <-- missing ,
]