0

I am working on a project for a Udemy course on learning Python. The goal is to create a tic tac toe game and whenever I run the script for some reason running the game when I chose where to put either marker on the board I get the error:

Traceback (most recent call last):
File "C:\Users\zanec\Desktop\Udemy_Python_course\practice\milestone-1-tic-
tac-toe-game.py", line 189, in <module>
player_input()
File "C:\Users\zanec\Desktop\Udemy_Python_course\practice\milestone-1-tic-
tac-toe-game.py", line 137, in player_input
Player_1_lucky()
File "C:\Users\zanec\Desktop\Udemy_Python_course\practice\milestone-1-tic-
tac-toe-game.py", line 36, in Player_1_lucky
if number%2 != 0:
TypeError: not all arguments converted during string formatting

Here is the code. Any feedback I will be grateful for thank you.

import random
from IPython.display import clear_output

#tries = 1
board = [0,1,2,3,4,5,6,7,8,9]
player_one = ('X')
player_two = ('O')

def display_board(board):

    print('     |     | ')
    print(' ',board[7],' | ',board[8],' | ',board[9])
    print('     |     | ')
    print('----------------')
    print('     |     | ')
    print(' ',board[4],' | ',board[5],' | ',board[6])
    print('     |     | ')
    print('----------------')
    print('     |     | ')
    print(' ',board[1],' | ',board[2],' | ',board[3])
    print('     |     | ')

def player_input():

    players_rand = 0 #random.randint(0,1)

    for number in board:

            if number == 0:
                print('Welcome to Tic Tac Toe!')

            else:
                if players_rand == 0:
                    def Player_1_lucky():

                        if number%2 != 0:
                            #def Player_1_turn():
                            answer1 = int(input('Player 1, assign the 
marker: X, to the board using a number from 1 to 9: '))
                            #answer_convert1 = int(answer1)

                            if answer1 == 1:
                                board[1] = ('X')
                                display_board(board)
                            #elif board[1] == ('X'):
                                #print('You cannot use this place because it 
is already taken. Please pick another number.')
                                #Player_1_turn()
                            if answer1 == 2:
                                board[2] = ('X')
                                display_board(board)
                            if answer1 == 3:
                                board[3] = ('X')
                                display_board(board)
                            if answer1 == 4:
                                board[4] = ('X')
                                display_board(board)
                            if answer1 == 5:
                                board[5] = ('X')
                                display_board(board)
                            if answer1 == 6:
                                board[6] = ('X')
                                display_board(board)
                            if answer1 == 7:
                                board[7] = ('X')
                                display_board(board)
                            if answer1 == 8:
                                board[8] = ('X')
                                display_board(board)
                            if answer1 == 9:
                                board[9] = ('X')
                                display_board(board)

                            #Player_1_turn()

                        elif number%2 == 0:
                            def Player_2_turn():
                                answer2 = int(input('Player 2, assign the 
marker: O, to the board using a number from 1 to 9: '))
                                #answer_convert2 = int(answer2)

                                if answer2 == 1 and board[1] == ('X'):
                                    #board[1] = ('X')
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 1:
                                    board[1] = ('O')
                                    display_board(board)
                                if answer2 == 2 and board[2] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 2:
                                    board[2] = ('O')
                                    display_board(board)
                                if answer2 == 3 and board[3] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 3:
                                    board[3] = ('O')
                                    display_board(board)
                                if answer2 == 4 and board[4] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 4:
                                    board[4] = ('O')
                                    display_board(board)
                                if answer2 == 5 and board[5] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 5:
                                    board[5] = ('O')
                                    display_board(board)
                                if answer2 == 6 and board[6] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 6:
                                    board[6] = ('O')
                                    display_board(board)
                                if answer2 == 7 and board[7] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 7:
                                    board[7] = ('O')
                                    display_board(board)
                                if answer2 == 8 and board[8] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 8:
                                    board[8] = ('O')
                                    display_board(board)
                                if answer2 == 9 and board[9] == ('X'):
                                    print('You cannot use this place because 
it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 9:
                                    board[9] = ('O')
                                    display_board(board)

                            Player_2_turn()

                    Player_1_lucky()    

                if players_rand == 1:

                    if number%2 != board[0]:
                        answer2 = input('Play   er 2, assign the marker: O, 
 to the board using a number from 1 to 9: ')
                        answer_convert2 = int(answer2)
                    else:
                        answer1 = input('Player 1, assign the marker: X, to 
the board using a number from 1 to 9: ')
                        answer_convert1 = int(answer1)

display_board(board)
player_input()
1
  • 2
    My guess is that number contains a string. Try to replace if number%2 != 0: for if int(number) % 2 != 0: Commented Feb 26, 2018 at 7:38

3 Answers 3

2

In these lines:

answer2 = int(input('Player 2, assign the marker: O, to the board using a number from 1 to 9: '))
#answer_convert2 = int(answer2)

the code is correctly converting the user's input from a string to an int. But lower down, in these lines:

answer2 = input('Player 2, assign the marker: O, to the board using a number from 1 to 9: ')
answer_convert2 = int(answer2)

the code is leaving answer2 as a string. That means that this line:

if number % 2 != 0:

sometimes performs the modulo operation you expect and tests for oddness (when it is an integer) and sometimes tries to do string interpolation (when it is a string).

Sign up to request clarification or add additional context in comments.

Comments

0

As Paulo Scardine has pointed out. The issue is the the values in number are changed as the game progresses. A simple fix for this could be to add an additional parameter called turns and use it to keep track of the players turns.

import random
from IPython.display import clear_output

#tries = 1
turns = range(10)
board = ["","","","","","","","","",""]
player_one = ('X')
player_two = ('O')

def display_board(board):

    print('     |     | ')
    print(' ',board[7],' | ',board[8],' | ',board[9])
    print('     |     | ')
    print('----------------')
    print('     |     | ')
    print(' ',board[4],' | ',board[5],' | ',board[6])
    print('     |     | ')
    print('----------------')
    print('     |     | ')
    print(' ',board[1],' | ',board[2],' | ',board[3])
    print('     |     | ')

def player_input():

    players_rand = 0 #random.randint(0,1)

    for number in turns:

            if number == 0:
                print('Welcome to Tic Tac Toe!')

            else:
                if players_rand == 0:
                    def Player_1_lucky():
                        print "===================="
                        print(number)
                        print "===================="
                        if number%2 != 0:
                            #def Player_1_turn():
                            answer1 = int(input('Player 1, assign the marker: X, to the board using a number from 1 to 9: '))
                            #answer_convert1 = int(answer1)

                            if answer1 == 1:
                                board[1] = ('X')
                                display_board(board)
                            #elif board[1] == ('X'):
                                #print('You cannot use this place because it is already taken. Please pick another number.')
                                #Player_1_turn()
                            if answer1 == 2:
                                board[2] = ('X')
                                display_board(board)
                            if answer1 == 3:
                                board[3] = ('X')
                                display_board(board)
                            if answer1 == 4:
                                board[4] = ('X')
                                display_board(board)
                            if answer1 == 5:
                                board[5] = ('X')
                                display_board(board)
                            if answer1 == 6:
                                board[6] = ('X')
                                display_board(board)
                            if answer1 == 7:
                                board[7] = ('X')
                                display_board(board)
                            if answer1 == 8:
                                board[8] = ('X')
                                display_board(board)
                            if answer1 == 9:
                                board[9] = ('X')
                                display_board(board)

                            #Player_1_turn()

                        elif number%2 == 0:
                            def Player_2_turn():
                                answer2 = int(input('Player 2, assign the marker: O, to the board using a number from 1 to 9: '))
                                #answer_convert2 = int(answer2)

                                if answer2 == 1 and board[1] == ('X'):
                                    #board[1] = ('X')
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 1:
                                    board[1] = ('O')
                                    display_board(board)
                                if answer2 == 2 and board[2] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 2:
                                    board[2] = ('O')
                                    display_board(board)
                                if answer2 == 3 and board[3] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 3:
                                    board[3] = ('O')
                                    display_board(board)
                                if answer2 == 4 and board[4] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 4:
                                    board[4] = ('O')
                                    display_board(board)
                                if answer2 == 5 and board[5] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 5:
                                    board[5] = ('O')
                                    display_board(board)
                                if answer2 == 6 and board[6] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 6:
                                    board[6] = ('O')
                                    display_board(board)
                                if answer2 == 7 and board[7] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 7:
                                    board[7] = ('O')
                                    display_board(board)
                                if answer2 == 8 and board[8] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 8:
                                    board[8] = ('O')
                                    display_board(board)
                                if answer2 == 9 and board[9] == ('X'):
                                    print('You cannot use this place because it is already taken. Please pick another number.')
                                    Player_2_turn()
                                elif answer2 == 9:
                                    board[9] = ('O')
                                    display_board(board)

                            Player_2_turn()

                    Player_1_lucky()    

                if players_rand == 1:

                    if number%2 != board[0]:
                        answer2 = input('Play   er 2, assign the marker: O,  to the board using a number from 1 to 9: ')
                        answer_convert2 = int(answer2)
                    else:
                        answer1 = input('Player 1, assign the marker: X, to the board using a number from 1 to 9: ')
                        answer_convert1 = int(answer1)

display_board(board)
player_input()

Comments

0

Thank you guys so much for your feedback. Because of your help, I figured that the board string while the numbers were being replaced with strings, the for loop couldn't properly function any longer while the game progressed because it relied on the numbers to decide which player's turn it is. The solution I came up with involved both your guys solutions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.