Python Forum
Trouble with global variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with global variables
#1
I am trying to make an experience system for my summer game project but for some reason it does not seems to carry over to the function of experience system. Here is what I did:

global currentxp
currentxp = 0
global level
level = 1
def exp_system():
    import random
    mobxp = random.randrange(1, 20, 1)
    currentxp = currentxp
    currentxp = currentxp + mobxp
    level = currentxp
    if currentxp >= 100:
        print("You Leveled Up!")
        level_up = True
    if currentxp < 100:
        level_up = False
        print("You need more xp to level up!")
    if level_up:
        currentxp = 0
        print("Your Current XP is now:")
        print(currentxp)
        level = level + 1
        print("You current level is now:")
        print(level)
        improvestst = input("Choose a stat to improve:(Health or Attack Power)")
    else:
        print("Your Current XP is now:")
        print(currentxp)
        print("You current level is now:")
        print(level)
To test the system I used the following:
mobhealth = 0
if mobhealth >= 0:
    victory = True
if victory:
    exp_system()
    victory = False
However I am getting UnboundLocalError: local variable 'currentxp' referenced before assignment on currentxp = currentxp on the Python console. I hope someone can tell me what I did wrong and give an example using my variables. Thanks!
Reply


Messages In This Thread
Trouble with global variables - by somerandomguy123 - Jul-03-2019, 09:47 PM
RE: Trouble with global variables - by Yoriz - Jul-03-2019, 10:03 PM
RE: Trouble with global variables - by ichabod801 - Jul-03-2019, 10:58 PM

Forum Jump:

User Panel Messages

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