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 5967159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:54:14+00:00 2026-05-22T19:54:14+00:00

I’ve pasted the code here: X = X O = O board = []

  • 0
I've pasted the code here:

X = "X"
O = "O"
board = []
EMPTY = ""

def instructions_prompt ():
    print "\t\t\tNoughts and Crosses"
    print \
        """Foolish human. Now that you've entered this Python program,
       there is no exit. None! (At any point in the game if you feel
       like you are intimidated by my presence, hit 0 to exit)

       This challenge of wits will be one of many failures in your life.

       The instructions are as follows:

       1. Select a number from the following key:

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

       to place X or O which is predetermined by whether or not you start
       the game.

       2. Try to save face while failing. And don't talk about Fight Club."""

def start_prompt ():
    choice = raw_input ("Would you like to go first (Y/N)?")
    lower = choice.lower()
    if lower == "y":
        human = X
        computer = O
        print "You're",human
        print "I am",computer
    elif lower == "n":
        computer = X
        human = O
        print "You're",human
        print "I'm in",computer
    return human, computer

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

def turn_switcher (turn):
    if turn == X:
        return O
   else:
        return X 

def fresh_board ():
    for i in range (9):
        board.append (EMPTY)
    return board

def request_move ():
    try:
        square = int(raw_input("Where'd you like your square to be?"))
        if square>8 or square<0:
            print "This program can't proceed as that number is not on the board."
        else:
            return square
    except:
        print "That's not a number on the board. This program can't proceed."

def legal_moves (board):
    legal_moves = []
    for i in range (9):
        if board [i] == EMPTY:
            legal_moves.append(i)
    return legal_moves

def winner (board):
    if board[0] == board [1] == board [2] != EMPTY:
        winner = board [0]
        return winner
    elif board [0] == board [3] == board [6] != EMPTY:
        winner = board [0]
        return winner
    elif board [0] == board [4] == board [8] != EMPTY:
        winner = board [0]
        return winner
    elif board [1] == board [4] == board [7] != EMPTY:
        winner = board [1]
        return winner
    elif board [2] == board [5] == board [8] != EMPTY:
        winner = board [2]
        return winner
    elif board [2] == board [4] == board [6] != EMPTY:
        winner = board [2]
        return winner
    elif board [6] == board [7] == board [8] != EMPTY:
        winner = board [8]
        return winner
    elif EMPTY not in board:
        return None

def human_move (board, square):
    legal = legal_moves(board)
    if square not in legal: 
        print "This is not a legal move"
        sys.exit()
    else:
        return square

def computer_move (computer, board, human):
    best = (4,0,8,2,6,1,3,5,7)
    board = board [:]
    legal = legal_moves(board)
    for i in legal:
        board[i] = computer
        if winner(board) == computer:
            return i
        board = EMPTY

#Stopping human from winning
    for i in legal_moves(board):
        board [i] = human
        if winner(board) == human:
             return i

    for i in best:
        if i in legal_moves(board):
            return i

def main ():
    instructions_prompt ()
    human, computer = start_prompt ()
    turn = X
    board = fresh_board()
    request_move ()
    display_board (board)

    while not winner(board):
        if turn == human:
            square = request_move()
            move = human_move (board, square)
            board[move] = human
        else:
            move = computer_move(computer, board, human)
            board[move] = computer
        display_board(board)
        turn = turn_switcher(turn)

main ()
raw_input ("Enter a key to end.")

TRACKBACK:

Traceback (most recent call last):
  File "C:\Users\COMPAQ\Desktop\NoughtsCrosses.py", line 152, in <module>
  main ()
  File "C:\Users\COMPAQ\Desktop\NoughtsCrosses.py", line 147, in main
  move = computer_move(computer, board, human)
  File "C:\Users\COMPAQ\Desktop\NoughtsCrosses.py", line 118, in computer_move
 board[i] = computer
 TypeError: 'str' object does not support item assignment 
  • 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-22T19:54:14+00:00Added an answer on May 22, 2026 at 7:54 pm

    You have your arguments mixed up for the function computer_move. On line 113, the arguments are in the order computer, board, human. However, on line 147, where computer_move is called, the order is board, computer, human.

    Python gave you the confusing index error because Python strings are really just lists of characters. E.g.

    >>> "foo"[2] == "o"
    True
    

    Update: You’re getting this new error because of the last line of computer_move. It should be board[i] = EMPTY rather than board = EMPTY.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.