0

I tried to run dis code (its a little game and now im adding the graphics but it gives me the 'UnboundLocalError:' and i dont realy know what im doing wrong

from graphics import *

import time

import random

a = 5

c = 0

ix = 1001

x = 2

def main():



while int(ix) != int(a):

win = GraphWin('Game', 500, 500)
shape = Rectangle(Point(499,499), Point(0,0))
shape.setOutline("red")
shape.draw(win)

shape = Rectangle(Point(200, 100), Point(300, 0))
shape.setOutline("black")
shape.draw(win)


    input = Entry(Point(250, 450), 10)
    input.draw(win)

    win.getMouse()


    ix = input.getText()
    testText = Text(Point(250, 50), ix)
    testText.draw(win)
    time.sleep(1)
    testText.setText('')
main()

As result i expect a loop in the graphic window where when the input ix = a the loop ends.

nbk
  • 45,398
  • 8
  • 30
  • 47
  • Possible duplicate of [referenced before assignment error in python](https://stackoverflow.com/questions/855493/referenced-before-assignment-error-in-python) – Alex_P Sep 30 '19 at 19:25

1 Answers1

0

The problem is that ix is a local variable in your main() function that shadows your global ix defined outside main(). To fix it you have several options:

  • Define ix inside main()
  • Make ix global with global ix
  • Pass ix to the function as parameter: main(ix)
  • Remove the main() function entirely and just put everything to the global scope
gnvk
  • 1,494
  • 12
  • 14