Quick Problem

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
 
L

Lars Yencken

Hello,

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

Try adding into the first line of your win function:

global uw, cw

Then Python knows to look outside your function to find those variables.

Lars
 
J

John Machin

Traceback (most recent call last):
File "C:\Documents and Settings\admin\Desktop\test\test.py", line 59,

"admin"? I strongly suggest you set up another user ID without
administrator privileges, and use that for the 99% of things that don't
need administrator privileges.

THEN you can fix your code as Lars has suggested.

Cheers,
John
 
C

continium

Thank you. That worked perfectly, I understand why it was not working
before because the variables were assigned within the function
definition only. And replying to John Machin, admin is the name of the
account I created on windows. The default administrator account in XP
is called simply 'administrator' which is the account one would use in
safe mode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top