Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnboundLocalError Problem
#1
So I am making my first game in python. It is a text RPG. Obviously I can't post the entire code on here, but I think this will be enough to hopefully get this solved.

So right now I am creating achievements. For example, I have one where defeating 10 enemies gives you an achievement titled "soldier". My problem is with this. I set my enemies defeated to 9 and then went into combat and defeated one more to see if the achievement would work. I got this error:
Error:
File "C:/Users/DESKTOP/.PyCharmCE2018.3/config/scratches/Text RPG.py", line 672, in earn_achievement if player.enemies_killed >= 10 and soldier_achievement == False: UnboundLocalError: local variable 'soldier_achievement' referenced before assignment
This is the code that it is referencing:

soldier_achievement = False
hunter_achievement = False
slayer_achievement = False
executioner_achievement = False
john_achievement = False
shopper_achievement = False
merchant_achievement = False
cheater_achievement = False
dedication_achievement = False
frich_achievement = False
frrich_achievement = False
fstich_achievement = False
explorer_achievement = False
def earn_achievement():
    if player.enemies_killed >= 10 and soldier_achievement == False:
        print("#######################")
        print("# Achievement Earned! #")
        print("#      'Soldier'      #")
        print("#######################")
        player.achievements.append('Soldier')
        soldier_achievement = True
        player.gold += 10
        prompt()
I've been looking at this for an hour and trying different things to get it to work, but nothing has changed. Anyone have any ideas? If you need more information I will gladly give some.
Reply
#2
By assigning to soldier_achievement inside earn_achievement, Python thinks the variable is local to the function (at the time the function is defined). Therefore, it can't see the assignment to the global soldier_achievement on line 1. But then when it gets to line 15 at run time, the local version hasn't been assigned yet, and gives you the error.

You would need to pass the global variable to the function as a parameter, and get a new value out as a return value. It would probably be better to have a dictionary with all of those global variables as key/value pairs. However, you should learn more about functions before moving forward. I would suggest the function text_adventure tutorials linked to in my signature, below.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How does UnboundLocalError work? deanhystad 3 1,633 Feb-25-2022, 01:21 AM
Last Post: bowlofred
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,855 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  exec + subprocess = UnboundLocalError paul18fr 6 3,441 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 2,227 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  UnBoundLocalError Seaninho 3 2,620 May-31-2020, 07:22 AM
Last Post: azajali43
  UnboundLocalError: local variable referenced before assignment svr 1 3,260 Dec-27-2019, 09:08 AM
Last Post: perfringo
  UnboundLocalError: local variable ' ' referenced before assignment d3fi 10 5,430 Sep-03-2019, 07:22 PM
Last Post: buran
  'Exception Has occured: UnBoundLocalError' caston 1 2,413 Jun-12-2019, 02:33 PM
Last Post: perfringo
  UnboundLocalError, how to fix it please? etrius 4 3,838 Jan-18-2018, 09:53 PM
Last Post: nilamo
  How to fix UnboundLocalError Tawnwen 5 5,269 Jan-07-2018, 05:38 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020