I wanted to challenge myself and make a tictactoe game from my beginner knowledge of Python. I'm most of the way through the code, after I get these variables to change and it to appear on the board I can add in o registration as well and get it on a while command or something to keep the game going until someone wins. The only problem I'm having is the variables wont change even though the conditions I've set are being met. If you start the game, go to the x team, and make a move to spot 1 or 2 then it should show x on that spot on the board. Although even when I print the variable afterwards outside of the board to see if it was something wrong with the it, it will still show the original string that was connected to the x2 or x1 variable. How would I go about fixing this?(importing random is for when I add AI to play against the player)
import random
row = " | | "
intersection = "---|---|---"
winner = None
draw = False
playerx = False
playero = False
x1 = " "
x2 = " "
x3 = " "
x4 = " "
x5 = " "
x6 = " "
x7 = " "
x8 = " "
x9 = " "
print("Welcome to my TicTacToe, which side would you like to be on? ")
def start():
player = str(input())
if player == "x":
print("You are x!")
playerx = True
return "x"
elif player == "o":
print("You are o!")
playero = True
return "o"
if start() == "x" or "o":
print("1-9 correspond to their spots on the board, input your move here: ")
def action():
x10 = int(input())
if x10 <= 9:
return x10
elif x10 >= 10:
print("Not a spot on the board! Try again!")
action()
print(action())
if start() == "x":
if action() == 1:
x1 = "x"
elif action() == 2:
x2 = "x"
elif action() == 3:
x3 = "x"
elif action() == 4:
x4 = "x"
elif action() == 5:
x5 = "x"
elif action() == 6:
x6 = "x"
elif action() == 7:
x7 = "x"
elif action() == 8:
x8 = "x"
elif action() == 9:
x9 = "x"
else:
print("Game broke, goodbye!")
def board():
print(f" {x1} | {x2} | {x3} ")
print(f"---|---|---")
print(f" {x4} | {x5} | {x6} ")
print(f"---|---|---")
print(f" {x7} | {x8} | {x9} ")
board()
I think I'm gonna need to add some timing code into this as well because sometimes it will bug out like it's trying to compute everything too fast as well but that's just something I haven't gotten to yet. Tips would also be appreciated.