When I run this code in Pwershell:
def treasure_room():
print "There is a pot of gold as high as you are tall, surrounded by pillars."
print "There is a sack by your feet. What do you do?"
sack = raw_input(prompt)
if sack == "pick up the sack" or sack == "pick it up" or sack == "pick up":
print "You pick up the sack."
nowsack()
else:
print "How will you carry the gold?!"
treasure_room()
def nowsack():
print "The pot is in front of you and you have the sack. Now what?"
gopot = raw_input(prompt)
if gopot == "walk to the pot" or gopot == "walk to it":
print "Now you are next to the pot. The pillars surround it and you..."
else:
"You continue to stand where you are, with the gold in front of you..."
nowsack()
treasure_room()
I run it in Prompt, and input "pick it up" once the second print function asks me "What do you do?"
I then get the following error in Powershell:
Traceback (most recent call last):
File "ex36game.py", line 58, in <module>
treasure_room()
File "ex36game.py", line 38, in treasure_room
nowsack()
UnboundLocalError: local variable 'nowsack' referenced before assignment
Anyone know why? I was thinking maybe I should put define nowsack() function above the first print comment "There is a pot of gold..." and indent the function with 8 spaces, but I tried that and kept getting errors (of a different kind.)
You need to unindent the nowsack()
function definition; move the whole block 4 spaces to the left:
def treasure_room():
print "There is a pot of gold as high as you are tall, surrounded by pillars."
print "There is a sack by your feet. What do you do?"
sack = raw_input(prompt)
if sack == "pick up the sack" or sack == "pick it up" or sack == "pick up":
print "You pick up the sack."
nowsack()
else:
print "How will you carry the gold?!"
treasure_room()
def nowsack():
print "The pot is in front of you and you have the sack. Now what?"
gopot = raw_input(prompt)
if gopot == "walk to the pot" or gopot == "walk to it":
print "Now you are next to the pot. The pillars surround it and you..."
else:
"You continue to stand where you are, with the gold in front of you..."
nowsack()
As you have indented it now, it is part of the treasure_room
function, and thus a local that is defined after you use it in the function. By un-indenting it it would become a global and the function is created when the module file is imported; it'll be available then by the time treasure_room()
is run.
The alternative would be for you to move the nowsack()
function definition to the top of the treasure_room()
function to make sure it is defined as a nested function before using it:
def treasure_room():
def nowsack():
print "The pot is in front of you and you have the sack. Now what?"
gopot = raw_input(prompt)
if gopot == "walk to the pot" or gopot == "walk to it":
print "Now you are next to the pot. The pillars surround it and you..."
else:
"You continue to stand where you are, with the gold in front of you..."
nowsack()
print "There is a pot of gold as high as you are tall, surrounded by pillars."
print "There is a sack by your feet. What do you do?"
sack = raw_input(prompt)
if sack == "pick up the sack" or sack == "pick it up" or sack == "pick up":
print "You pick up the sack."
nowsack()
else:
print "How will you carry the gold?!"
treasure_room()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.