[Tutor] Help

Dave Angel 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
>> #APOC
>> global ammo
>> global health
>> global lives
>> global exp
>> global food
>> ammo=55
>> health = 100
>> lives=10
>> 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.
>> Huh."
>>     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'
>> button.")
>>     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"
>>             ammo=ammo-15
>>             food=food-10
>>             return answer3
>>         if answer2 == "FLEE" or answer2 == "Flee" or answer2 == "flee":
>>             print "He shot you"
>>             lives=lives-1
>>         else:
>>             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"
>>     else:
>>         print "You didn't type Show or run away."
>>         part1()
>>
>> part1()
>>
>>
>> Here is my error, if it helps
>>
>> Traceback (most recent call last):
>>   File "C:\Users\Jack\Desktop\game2.py", line 45, in <module>
>>     part1()
>>   File "C:\Users\Jack\Desktop\game2.py", line 28, in part1
>>     ammo=ammo-15
>> UnboundLocalError: local variable 'ammo' referenced before assignment
>>
>>
>> Thanks,
>> 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
the doc-string.

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":



-- 

DaveA



More information about the Tutor mailing list