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()
if number%2 != 0:forif int(number) % 2 != 0: