d at davea.name
Thu Sep 6 10:51:51 CEST 2012
On 09/03/2012 10:39 AM, Jack Little wrote:
>> Ok, I am somewhat new to python, and I am making a text-based RPG. I get a
>> weird error with this code:
>> #A Python text-RPG
>> #A Jak Production
>> global ammo
>> global health
>> global lives
>> global exp
>> global food
>> health = 100
>> exp = 0
>> food = 30
>> def part1():
>> print "50 Days After The Outbreak:You are standing outside of the Empire
>> State Building."
>> print "Vines, plants, dirt, and grime cover its once-regal surfaces.
>> print "I guess if 80% of the world is dead, more people are concerned
>> about survival than sightseeing.God."
>> print "Generally,us survivors tend to band together. Mostly it is for
>> good. Not the bandits."
>> print " Bandit:'Hey you! What you got in that bag?"
>> print "I recognized this Bandit as Sam Cabelo. He was the janitor at my
>> office building. Not the nicest fellow."
>> answer = raw_input("Type 'show' or 'run away' then hit the 'Enter'
>> if answer == "SHOW" or answer == "Show" or answer == "show":
>> print "Ahhh. Nice .45 you got there. And some food.Hand it over.(He
>> says this like a threat, reinforced by that revolver in his hand"
>> answer2 = raw_input("Type either Hand it over or flee")
>> if answer2 == "HAND IT OVER" or answer2 == "Hand it over" or answer2
>> == "hand it over":
>> print "Bandit: Good Job.. Go on now"
>> return answer3
>> if answer2 == "FLEE" or answer2 == "Flee" or answer2 == "flee":
>> print "He shot you"
>> print "TYPE SOMETHING CORRECTLY"
>> return part1
>> elif answer == "run away" or "Run Away" or "RUN AWAY":
>> print "He shot you... hee hee hee"
>> print "When the input comes up again, type a differet answer"
>> print "You didn't type Show or run away."
>> Here is my error, if it helps
>> Traceback (most recent call last):
>> File "C:\Users\Jack\Desktop\game2.py", line 45, in <module>
>> File "C:\Users\Jack\Desktop\game2.py", line 28, in part1
>> UnboundLocalError: local variable 'ammo' referenced before assignment
>> Jack Little
Your global declarations are all at top-level scope, where they are
meaningless. All variables defined there are global. What you want for
ammo is a global declaration inside the function. By convention, you
declare any globals at the top of the function, right after the def and
You'll also need global declarations for food and lives, and perhaps
others I didn't notice.
Other things I notice:
You omitted the parentheses in one reference to part1. So it doesn't
get called there.
You try to use those part1() calls as some form of looping construct.
Instead you should use a while loop or similar. That also will avoid
the problem you've got now where if a person types nonsense to the
second question, he ends up being asked the first question again.
Use lower() method on strings to avoid needing multiple comparisons:
if answer2.lower() == "flee":
More information about the Tutor