C
continium
I am learning how to program and I only started about a month ago so
the answer to this is probably quite obvious. I'm running accross a
problem when writing a rock, paper, scissor program. Although I can
write such program in 5 minutes I want to write a program into which I
will implement many other features such as logging in, checking past
games and such.
I have an if, elif construction determening the outcome possible(not a
tie though) in everyone of these either the user or the computer wins.
Here are the errors I get:
Traceback (most recent call last):
File "C:\Documents and Settings\admin\Desktop\test\test.py", line 59,
in -toplevel-
win(2)
File "C:\Documents and Settings\admin\Desktop\test\test.py", line 31,
in win
uw = uw + 1 # user win
UnboundLocalError: local variable 'uw' referenced before assignment
Here is my code:
cw = 0 #Computer wins total
uw = 0 # User wins total
def win(who):
if who == 1:
cw = cw + 1 # computer win
elif who == 2:
uw = uw + 1 # user win
l = ['r', 'p', 's']
def rand():
random.shuffle(l)
while 1 == 1:
rand()
cc = l[0] # Computer Choice
uc = raw_input('[r]ock, [p]aper,cissor') # User Choice
if cc == uc:
print 'Tie, try again'
continue
else:
break
if cc == 'r' and uc == 's':
win(1)
print 'lose'
elif cc == 'r' and uc == 'p':
win(2)
print 'win'
elif cc == 'p' and uc == 'r':
win(1)
print 'lose'
elif cc == 'p' and uc == 's':
win(2)
print 'win'
elif cc == 's' and uc == 'r':
win(2)
print 'win'
elif cc == 's'and uc == 'p':
win(1)
print 'lose'
else:
print 'ELSE ERROR'
print 'uw = ', uw
print 'cw = ', cw
the answer to this is probably quite obvious. I'm running accross a
problem when writing a rock, paper, scissor program. Although I can
write such program in 5 minutes I want to write a program into which I
will implement many other features such as logging in, checking past
games and such.
I have an if, elif construction determening the outcome possible(not a
tie though) in everyone of these either the user or the computer wins.
Here are the errors I get:
Traceback (most recent call last):
File "C:\Documents and Settings\admin\Desktop\test\test.py", line 59,
in -toplevel-
win(2)
File "C:\Documents and Settings\admin\Desktop\test\test.py", line 31,
in win
uw = uw + 1 # user win
UnboundLocalError: local variable 'uw' referenced before assignment
Here is my code:
cw = 0 #Computer wins total
uw = 0 # User wins total
def win(who):
if who == 1:
cw = cw + 1 # computer win
elif who == 2:
uw = uw + 1 # user win
l = ['r', 'p', 's']
def rand():
random.shuffle(l)
while 1 == 1:
rand()
cc = l[0] # Computer Choice
uc = raw_input('[r]ock, [p]aper,
if cc == uc:
print 'Tie, try again'
continue
else:
break
if cc == 'r' and uc == 's':
win(1)
print 'lose'
elif cc == 'r' and uc == 'p':
win(2)
print 'win'
elif cc == 'p' and uc == 'r':
win(1)
print 'lose'
elif cc == 'p' and uc == 's':
win(2)
print 'win'
elif cc == 's' and uc == 'r':
win(2)
print 'win'
elif cc == 's'and uc == 'p':
win(1)
print 'lose'
else:
print 'ELSE ERROR'
print 'uw = ', uw
print 'cw = ', cw