0

This may be a fairly noob-ish question but it is a problem I have ran into before and can't seem to fathom a way around.

in this instance, I am writing a small game and I need to increment two variables, all of which will be apparent in the code, but no matter what I do, I keep getting a 'variable referenced before assignment' error. this happens even though I have globally declared the variables before I try to reference them.

may apologies if this problem has a simple solution but any and all advise is appreciated.

here is the code:

global level, score
level = 0
score = -1

def play():
    level += 1
    score = (score + level)
    game()
    return

def close():
    win.destroy()
    return

def green():
    return

def red():
    return

def yellow():
    return

def blue():
    return

def game():

    levelBox.delete(1,END)
    scoreBox.delete(1,END)
    levelBox.insert(1, level)
    scoreBox.insert(1, score)
    win.after(2000, play)
    return

additional information:

  • the program uses a Tkinter interface

  • the colour functions have no use yet

  • I have already tried where the variables are declared

  • the 'win.after' function can't assign arguments to the function it calls

Jeff B
  • 8,572
  • 17
  • 61
  • 140
  • Read one or two answers in the duplicate, they contain very clear explanations of what's wrong with your code. – timgeb May 15 '17 at 11:47
  • I've rolled back your edit since it really should have been a comment on your question. Python doesn't let you modify the value of global variables without intentionally saying that you meant to do it. See the answers to the duplicate question; it explains when you need to use `global` inside a method to allow your code inside a method to modify a global variable. – Jeff B May 15 '17 at 14:44

0 Answers0