| | |
UnboundLocalError
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
This is probably an easy problem to solve but I'm having a bit of difficulty. The error is UnboundLocalError: local: "switch" and that's all the error says. The line refers to the line switch = - switch in the following code:
I used global so I don't know why I'm getting this error.
Python Syntax (Toggle Plain Text)
boundary = game.getBoundary() game.setBackground(java.awt.Color.black) level = 1 maxX = -1 maxItem = 0 minX = boundary.getWidth()+1 minItem = 0 change = 0 switch = 1 class shootCollisionListener (events.CollisionListener): global switch def __init__ (self, item): self._item = item def collisionDetected(self, event): if(event.getSource()==boundary): switch = -switch elif(event.getSource().getType()=="projectile"): game.removeElement(event.getSource()) game.removeElement(self._item)
I used global so I don't know why I'm getting this error.
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
When variables are assigned inside a function, they are always bound to the function's local namespace. The global namespace is never checked for name switch, therefore the error.
Example:
Example:
Python Syntax (Toggle Plain Text)
>>> switch = -1 >>> def test(): ... return -switch ... >>> switch = test() >>> switch 1 >>> def test1(): ... switch = -switch ... return switch ... >>> test1() Traceback (most recent call last): File "<interactive input>", line 1, in ? File "<interactive input>", line 2, in test1 UnboundLocalError: local variable 'switch' referenced before assignment >>> def test2(): ... global switch ... switch = -switch ... return switch ... >>> test2() -1 >>> test2() 1 >>>
![]() |
Similar Threads
- How would I make a text adventure? (Python)
- recalling functions (Python)
- Calling Shell Commands (Python)
- problem on len() (Python)
Other Threads in the Python Forum
- Previous Thread: Python, Cgi and MySQL
- Next Thread: Idle, wxPython, Tkinter, Widget toolkit, PyGTK, PyQT, Tk GUI?
| Thread Tools | Search this Thread |
accessdenied apache application argv array beginner book change code color converter countpasswordentry dan08 dictionary dynamic edit editing enter examples excel file filename float format function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython





