[Tutor] namespaces
Robert Johansson
robert.johansson at math.umu.se
Sun May 30 17:39:45 CEST 2010
Hi,
This code generates the message "UnboundLocalError: local variable 'doubles'
referenced before assignment" (line: if d[0] == d[1] and doubles == 2:)
http://pastebin.com/mYBaCfj1
I think I have a fair picture of what it means but I would be very happy if
someone could explain the difference between the two variables h and doubles
in the code. Why is one accessible from the function but not the other? I
looked into rules for namespaces but I'm still confused. Below is another
sample of the code
Cheers, Robert
from random import *
h = 6
doubles = 0 # current number of consecutive doubles
def roll():
d = [randint(1, h), randint(1, h)]
if d[0] == d[1] and doubles == 2:
doubles = 0
return 0
elif d[0] == d[1] and doubles < 2:
doubles += 1
return sum(d)
else:
return sum(d)
for n in range(10):
d = roll()
print d
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100530/9283c4cd/attachment-0001.html>
More information about the Tutor
mailing list