mardi 3 février 2015

Python - Local Variable Error

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.)


Aucun commentaire:

Enregistrer un commentaire