469,915 Members | 2,508 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,915 developers. It's quick & easy.

Quick Problem

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, [s]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

Apr 19 '06 #1
3 1057
Hello,

On Tue, 18 Apr 2006 19:05:06 -0700, co*******@gmail.com wrote:
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
Apr 19 '06 #2
On 19/04/2006 12:05 PM, co*******@gmail.com wrote:
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
Apr 19 '06 #3
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.

Apr 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.