Reputation: 65
I'm trying to create a very basic tic tac toe game that takes a user input, and depending on that input, places either an x or o down in the index of that input. I'm relatively new to python so I'm trying not to google the answer but really break each step down.
#Create Board
row1 = ['', '', '']
row2 = ['', '', '']
row3 = ['', '', '']
def display(x,y,z):
print(x)
print(y)
print(z)
show_board = display(row1,row2,row3)
#Get user input
def userChoice():
choice = int(input('Pick a number 1-9! '))
while choice > 9 or choice < 1:
print('Please pick a number 1-9!')
choice = int(input('Pick a number 1-9! '))
return choice
#Place the marker onto the board
def placeMarker():
if position >= 1 and position < 4:
row1[position] = marker
elif position >= 4 and position < 7:
row2[position] = marker
else:
row3[position] = marker
#Start the game
def startGame():
clear_output()
marker = input('x or o? ').lower()
# while marker != 'x' or marker != 'o':
# marker = input('x or o?').lower()
display(row1,row2,row3)
while(True):
userChoice()
placeMarker()
display(row1,row2,row3)
I created a function to place the marker down and it does so on the first attempt, but any further attempt wont update the board. I feel like it's an issue with my placeMarker() function but can't really hit the nail on the head here. Any help or advice would be greatly appreciated!
Upvotes: 0
Views: 38
Reputation: 781888
placeMarker()
is not correct. First of all, list indexes start at 0
, not 1
, so you need to subtract 1
from position
to get a valid index. So any value of position
other than 1
or 2
would get an IndexError
.
In addition, it's wrong when the position is in row2
or row3
, because you need to adjust the position down from a higher number to the 0-2
range.
You also have no code that ever sets position
, and marker
is a local variable in startGame()
. You need to add more function returns and parameters.
Finally, you need to call startGame()
to get everything going.
#Create Board
row1 = ['', '', '']
row2 = ['', '', '']
row3 = ['', '', '']
def display(x,y,z):
print(x)
print(y)
print(z)
show_board = display(row1,row2,row3)
#Get user input
def userChoice():
choice = int(input('Pick a number 1-9! '))
while choice > 9 or choice < 1:
print('Please pick a number 1-9!')
choice = int(input('Pick a number 1-9! '))
return choice
#Place the marker onto the board
def placeMarker(position, marker):
if position < 4:
row1[position-1] = marker
elif position < 7:
row2[position-4] = marker
else:
row3[position-7] = marker
#Start the game
def startGame():
clear_output()
marker = input('x or o? ').lower()
# while marker != 'x' or marker != 'o':
# marker = input('x or o?').lower()
display(row1,row2,row3)
while(True):
position = userChoice()
placeMarker(position, marker)
display(row1,row2,row3)
startGame()
Upvotes: 1
Reputation: 181
You only defined the function startGame
but you did not called it.
Add startGame()
at the end of the program to call the function.
Note : the "first attempt" is done when you create the variable show_board
Upvotes: 1