PMARINA
PMARINA

Reputation: 304

Tuple index out of range

print "\n\t\t\t\t",moves[6],"|",moves[7],"|",moves[8]

IndexError: tuple index out of range

is the error that I am getting. I am currently writing a python program for tictactoe. moves is equal to ['','','','','','','','','']. Would you like any other info?

I have so far, changed 789 in the row to 678 because indexes begin at 0. Nothing happening.

Next, I tried various other small changes, each of which either changed the error or just gave me the same error.

I also attempted to change formats and things, which did not go so well. I am running python 2.7(?), if that matters.

def draw(moves):

print "\n\t\t\t\t",moves[6],"|",moves[7],"|",moves[8]
print "\t\t\t\t", "--------"
print "\n\t\t\t\t",moves[3],"|",moves[4],"|",moves[5]
print "\t\t\t\t", "--------"
print "\n\t\t\t\t",moves[0],"|",moves[1],"|",moves[2], "\n"




   import time
import random
moves = ['','','','','','','','','']
player = ''
ai = ''
restart = ''
first = 19
whosfirst = 1
# Ok, so this is to decide the symbol for each person ---------------------------------------------------
def XorO(man, machine):
    print 'Please select a symbol to represent you'
    player = raw_input( "Please select \"X\" or \"O\"")
    while player not in ('x','X','o','O'):
        print "I'm sorry, I don't think I got that. Please try again"
        time.sleep(1)
        player = raw_input("Select \"X\" or \"O\"")
    if player == 'x' or player == 'X':
        print "X"
        time.sleep(1)
        print "Ok, then I'll be \"O\""
        ai = 'o'
    else:
        print "O"
        time.sleep(1)
        print "Ok, then I'll be \"X\""
        ai = 'x'
    return player.upper(), ai.upper()
# This is for who is going first -----------------------------------------------------------------------
def first():
    number = "a"
    while number not in ('n','y',"no",'yes'):
        number = raw_input("Do you want to go first? - ").lower()
        if number == 'y' or number == 'yes':
            return 1
        elif number == 'n' or number == 'no':
            return 0
        else:
            print "I'm sorry, I dont think that I understood you."
            time.sleep(1)
            print "Please select y, yes, n, or no"
#Here Comes the hard part -- drawing the board
def draw(moves):

    print "\n\t\t\t\t",moves[6],"|",moves[7],"|",moves[8]
    print "\t\t\t\t", "--------"
    print "\n\t\t\t\t",moves[3],"|",moves[4],"|",moves[5]
    print "\t\t\t\t", "--------"
    print "\n\t\t\t\t",moves[0],"|",moves[1],"|",moves[2], "\n"

def playerwon():
    print "You won! Yay! Now try again"

def aiwon():
    print "I won! Won't you try again?"

def playerfirst(player, ai, moves):
    while win(player, ai, moves) is None:
        moves = playermove(player, moves - 1)
        moves[int(moves)] = player
        draw(moves)
        if win(player, ai, moves) != None:
            break
        else:
            pass
        Dmove = machine_move(player, ai, moves)
        print "Nice moves! I'll try ",Dmove
        moves[int(Dmove)] = ai
        draw(moves)
    q = win(player, ai, moves)
    if q == 1:
        playerwon()
    elif q == 0:
        aiwon()
    else:
        print "We tied =-("
        time.sleep(2)
        print "Let's have another go!"



def aifirst(player, ai, moves):
    while not win(player, ai, moves):
        Dmove = machine_move(man, machine, moves)
        print "I'll take..." , Dmove
        moves[Dmove] = machine
        draw(moves)
        if win(player, ai, moves) != None:
            break
        else:
            pass
        moves = playermove(player, moves)
        moves[int(moves)] = player
        draw(moves)
    variable = win(player, ai, moves)
    if q == 1:
        playerwon()
    elif q == 0:
        aiwon()
    else:
        print "We tied =-("
        time.sleep(2)
        print "Let's have another go!"


def win(player, ai, moves):
    ways = ((7,8,9),(4,5,6),(1,2,3),(7,4,1)(8,5,2),(9,6,3),(7,5,3),(9,5,1))
    for i in ways:
        if ways[i[0]] == ways[i[1]] == ways[i[2]] != empty:
            winner = new[i[0]]
            if winner == player:
                return 1
            elif winner == ai:
                return 0
            if empty not in new: 
                return 'TIE'
    if empty not in new: 
        return 'TIE'    
    return None



def playermove(player, moves): 
    moves = raw_input("Where would you like to place your symbol?")
    while True:
        if moves not in ('0','1','2','3','4','5','6','7','8'):
            print "Sorry, I don't think that's a valid spot... Try again"
            moves = raw_input("Where would you like to place your symbol ")
        elif moves[int(moves)] != empty:
            print "Sorry, I think there's somehing already there..."
            moves = raw_input("Where would you like to place your symbol?")
        else:
            return int(moves)




def aimove(player, ai, moves):
    bestmoves = [5, 7, 9, 1, 3]
    blank = []
    for i in range(0,9):
        if moves[i] == empty:
            blank.append(i)

    for num in blank:
        moves[i] = ai
        if win(man, ai, moves) is 0:

            return i
        moves[i] = empty

    for num in blank:
        moves[i] = man
        if win(man, ai, moves) is 1:

            return num
        moves[i] = empty

    return int(blank[random.randrange(len(blank))])




def display_instruction():
      print "Welcome to PMARINA's TicTacToe v 1.1.1 ..."
      print "In order to place your symbol, just press the corresponding key on your numpad."
      print "If you do not have a numpad, then please note down the following arrangement"
      print  "7 | 8 | 9"
      print  "-----------"
      print  "4 | 5 | 6"
      print  "-----------"
      print  "1 | 2 | 3"
      print "Good Luck, and don't forget to do the most important thing:"
      time.sleep(2)
      print "HAVING FUN!!"
      time.sleep(2)


def letsgo(player, ai, moves):
    display_instruction()
    print "so lets begin.."
    moves = XorO(player, ai)
    player = moves[0]
    ai = moves[1]
    whosfirst = first()
    if whosfirst == 1:
        print "Ok, you are first!"
        print "Lets go!"
        draw(moves)
        playerfirst(player, ai, moves)
    else:
        print "Ok, I'll be the first!"
        print "So, lets start.."
        draw(moves)
        aifirst(player, ai, moves)


letsgo(player, ai, moves)
raw_input("Press enter to exit")

Upvotes: 0

Views: 2575

Answers (3)

Lavish
Lavish

Reputation: 36

Line 14:

moves = ['','','','','','','','','']

Line 192:

moves = XorO(player, ai)
...

    draw(moves)

You overwrote your initial declaration of moves, so ('X', 'O') is being passed to draw.

Upvotes: 1

GreySage
GreySage

Reputation: 1180

The only tuple I see in the code is ways. Also you don't seem to initialize new before you use it in the function 'win'. FYI, tuples are made using parentheses tup = (1, 2, 3, 4) while lists use brackets list = [1,2,3,4]. Tuples are also immutable, so you can't modify them later.

Upvotes: 1

mpen
mpen

Reputation: 282845

If moves is ['','','','','','','','',''] and the error message you're getting is:

IndexError: tuple index out of range

Then the error is not occurring on the line you say it is. moves is a list, not a tuple. And it does have indices up to 8. moves[9] would generate this error instead:

IndexError: list index out of range

Upvotes: 1

Related Questions