Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6972211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:56:20+00:00 2026-05-27T16:56:20+00:00

So I am a total noob at python so im sorry if im not

  • 0

So I am a total noob at python so im sorry if im not being specific enough.

I am writing a tic tac toe program and the hardest thing is getting to switch turns

so i wrote a function to do this but i cant get the “turn” variable which belongs to another function. Im sorry if im being moronic but this is the best i can explain it. Thanks for your Time 🙂

BTW here is the code

x="X"
o="O"
EMPTY=" "
TIE="TIE"
NUM_SQUARES=9

def display_instruct():
    print(
    """
    Welcome to the greatest challenge ever

    Tic
    Tac 
    Toe

    the board is as shown

    0|1|2
    ------
    3|4|5
    -----
    6|7|8


    Prepare as teh game is about to start
    """)

def yesno(question):
    response=None
    while response not in ("y","n"):
        response=input(question).lower()
    return response
def asknum(question, low, high):
    response=None
    while response not in range(low,high):
        response=int(input(question))
    return response

def pieces():
    go_first=yesno("Do you want the first move")

    if go_first=="y":
        print("then take the first move you will need it")
        human=x
        computer=o
        turn=x
        return computer, human
    else:
        print ("Ok i shall go first")
        computer=x
        human=o
        turn=x
        return computer, human



def new_board():
    board=[]
    for square in range(NUM_SQUARES):
        board.append(EMPTY)
    return board

def display_board(board):
    print ("\n")
    print (board[0],"|",board[1],"|",board[2])
    print ("----------")
    print (board[3],"|",board[4],"|",board[5])
    print ("----------")
    print (board[6],"|",board[7],"|",board[8])

def legal_moves(board):
    moves=[]
    for square in range(NUM_SQUARES):
        if board[square]==EMPTY:
            moves.append(square)
    return moves

def winner(board):
    WAYS_TO_WIN=((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))

    for row in WAYS_TO_WIN:
        if board[row[0]]==board[row[1]]==board[row[2]]!=EMPTY:
            winner=[row[0]]
            return winner
        if EMPTY not in board:
            return TIE
        return None



def human_move(board,human):
    legal=legal_moves(board)
    move=None
    print (legal)
    while move not in legal:
        move=asknum("Where will you move",0,NUM_SQUARES)
    return move

def computer_move(board,computer,human):
    board=board[:]
    BEST_MOVES=(4,0,2,6,8,1,3,5,7)
    print ("I shall take square number", end=" ")
    for move in legal_moves(board):
        board[move]=computer
        if winner(board)==computer:
            print(move)
            return move
    for move in legal_moves(board):
        board[move]=human
        if winner(board)==human:
            print(move)
        return move

    board[move]=EMPTY

    for move in BEST_MOVES:
        if move in legal_moves(board):
            print (move)
        return move

def next_turn(turn):
    if turn==x:
        return o
    else:
        return x
def congrat_winner(the_winner,computer,human):
    if the_winner!=TIE:
        print(the_winner, "WON")
    else:
        print ("Its a ttie")
    if the_winner==computer:
        print ("I WON HA HA IN YO FACE")
    elif the_winner==human:
        print ("NO IT CANNOT BE")
    elif the_winner==TIE:
        print ("you were lucky this time")
def curturn(turn,computer,human):

    if turn==x and move!="" and move!=" ":
        turn=o
    elif turn==o and move!="" and move!=" ":
        turn=x


def main():
    display_instruct()

    computer, human=pieces()
    turn=x
    board=new_board()
    display_board(board)
    curturn(turn,computer,human)
    while not winner (board):
        display_board(board)


        if turn == human:
            move=human_move(board,human)
            board[move]=human   
        else:
             move=computer_move(board,computer,human)
             board[move]=computer
             display_board(board)
             turn=next_turn(turn)
             the_winner=winner(board)
             congrat_winner(the_winner,computer,human)




main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:56:21+00:00Added an answer on May 27, 2026 at 4:56 pm

    Functions look like this (simplified):

    def functionname(parameter1, parameter2=foo):
        code that does stuff
        return value
    

    If you in your function want to access a variable from the calling function, you pass it in as a parameter. If you from the calling function want to access a variable from the function you call, you return it.

    You can also use global variable (which you do, I see) and that is a Bad Idea, as it will make your program messy and hard to debug. So avoid that if you can (although in a small program like this, it’s not a disaster, so don’t bother now).

    If you want to access variables from completely different places, you probably want to look into object-oriented programming, with classes and stuff.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Total noob question here, but not finding the answer via search. What is the
Calling concat on vectors returns a list. Being a total noob I would expect
I am a noob to Python and have not had any luck figuring this
I'm a total noob to iPhone programming, and I've run into an exception being
I'm a total python noob so please bear with me. I want to have
I'm a total noob at workflow! I want to host several workflows ( not
Sorry if this is a total noob question, but I wanted to try to
Total noob to anything lower-level than Java, diving into iPhone audio, and realing from
Total noob question, but here. CSS .product__specfield_8_arrow { /*background-image:url(../../upload/orng_bg_arrow.png); background-repeat:no-repeat;*/ background-color:#fc0; width:50px !important; height:33px
Hey! Total CakePHP noob here. Updated at bottom / This is sort of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.