I have a problem calling up my compturn() function. Here is my code:

import random

def compturn():
	print 'Computer turn:'
	comp = str(random.randint(1, 9))
	if comp == a:
		compturn()
	elif comp == b:
		compturn()
	elif comp == c:
		compturn()
	elif comp == d:
		compturn()
	elif comp == e:
		compturn()
	elif comp == f:
		compturn()
	elif comp == g:
		compturn()
	elif comp == h:
		compturn()
	elif comp == i:
		compturn()
	else:
		board = board.replace(str(comp), "O")
		print board

print """ I have yet to make a device that tells you if you win but enjoy!"""

board = """ 1 | 2 | 3
---+---+---
 4 | 5 | 6
---+---+---
 7 | 8 | 9 """
print board

move1 = raw_input('Where do you want to place your X? ')
a = b = c = d = e = f = g = h = i = move1
board = board.replace(move1, "X")
print board

compturn()

b = comp

move2 = raw_input('Where do you want to place your X? ')
c = d = e = f = g = h = i = move1
board = board.replace(move2, "X")
print board

compturn()

d = comp

move3 = raw_input('Where do you want to place your X? ')
e = f = g = h = i = move3
board = board.replace(move3, "X")
print board

compturn()

f = comp

move4 = raw_input('Where do you want to place your X? ')
g = h = i = move4
board = board.replace(move4, "X")
print board

compturn()

h = comp

move5 = raw_input('Where do you want to place your X? ')
i = move5
board = board.replace(move5, "X")
print board

And here is what I get when I run it:

Last login: Mon Nov 28 18:40:45 on ttys000
/var/folders/fL/fL1lGxccGCypPBSIUXNGZGpkwIg/-Tmp-/Cleanup\ At\ Startup/a-344223770.900.py.command ; exit;
SHS-PA-47829M$:~ hovest.seth11$ /var/folders/fL/fL1lGxccGCypPBSIUXNGZGpkwIg/-Tmp-/Cleanup\ At\ Startup/a-344223770.900.py.command ; exit;
I have yet to make a device that tells you if you win but enjoy!
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
Where do you want to place your X? 2
1 | X | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
Computer turn:
Traceback (most recent call last):
File "/private/var/folders/fL/fL1lGxccGCypPBSIUXNGZGpkwIg/-Tmp-/Cleanup At Startup/a-344223770.899.py", line 42, in <module>
compturn()
File "/private/var/folders/fL/fL1lGxccGCypPBSIUXNGZGpkwIg/-Tmp-/Cleanup At Startup/a-344223770.899.py", line 25, in compturn
board = board.replace(str(comp), "O")
UnboundLocalError: local variable 'board' referenced before assignment
logout

[Process completed]


Any solutions?

The main problem is that you don't pass board to the function, update it, and then return the new board. Consider using a list instead of a string as it makes things much easier. Also use a loop to get the input instead of 5 separate sets of statements.

import random
 
def compturn(board):
    print 'Computer turn:'
    comp = str(random.randint(1, 9))
    while comp not in board:     ## occupied
        comp = str(random.randint(1, 9))
    """
	if comp == a:
		compturn()
	elif comp == b:
		compturn()
	elif comp == c:
		compturn()
	elif comp == d:
		compturn()
	elif comp == e:
		compturn()
	elif comp == f:
		compturn()
	elif comp == g:
		compturn()
	elif comp == h:
		compturn()
	elif comp == i:
		compturn()
	else:
    """
    board=board.replace(comp, "O")
    print "computer moves to square", comp
    print board
    return board
 
print """ I have yet to make a device that tells you if you win but enjoy!"""
 
board = """ 1 | 2 | 3
---+---+---
 4 | 5 | 6
---+---+---
 7 | 8 | 9 """
print board
 
move1 = raw_input('Where do you want to place your X? ')
#a = b = c = d = e = f = g = h = i = move1
board = board.replace(move1, "X")
print board
 
board=compturn(board)
 
#b = comp
 
move2 = raw_input('Where do you want to place your X? ')
#c = d = e = f = g = h = i = move1
board = board.replace(move2, "X")
print board
 
board=compturn(board)
 
#d = comp
 
move3 = raw_input('Where do you want to place your X? ')
#e = f = g = h = i = move3
board = board.replace(move3, "X")
print board
 
board=compturn(board)
 
#f = comp
 
move4 = raw_input('Where do you want to place your X? ')
#g = h = i = move4
board = board.replace(move4, "X")
print board
 
board=compturn(board)
 
#h = comp
 
move5 = raw_input('Where do you want to place your X? ')
i = move5
board = board.replace(move5, "X")
print board
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.