Python UnboundLocalError: local variable referenced before assignment - python

I'm confused. What is different about player1_head compared to the other variables I am printing in the code below? As far as I can tell it should behave the same as the others - it's declared in the global scope, no? I don't think it's a typo.
UnboundLocalError: local variable 'player1_head' referenced before assignment
from turtle import *
from random import randint
from utils import square, vector
player1_xy = vector(-100, 0)
player1_aim = vector(4, 0)
player1_body = []
player1_head = "It looks like I'm assigning here."
def draw():
"Advance player and draw game."
print("xy: ", player1_xy)
print("head: ", player1_head)
print("body: ", player1_body)
player1_xy.move(player1_aim)
player1_head = player1_xy.copy()
player1_body.append(player1_head)
square(player1_xy.x, player1_xy.y, 3, 'red')
update()
ontimer(draw, 200)
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
draw()
done()

Because you failed to declare player1_head as a global, in the draw() function it appears to that function that you're printing out local variable player1_head before it has a value:
print("head: ", player1_head)
# ...
player1_head = player1_xy.copy()
Instead do:
def draw():
""" Advance player and draw game. """
global player1_head
print("xy: ", player1_xy)
print("head: ", player1_head)
print("body: ", player1_body)
player1_xy.move(player1_aim)
player1_head = player1_xy.copy()
player1_body.append(player1_head)
square(player1_xy.x, player1_xy.y, 3, 'red')
update()
ontimer(draw, 200)

The assignment player1_head = player1_xy.copy() in the draw() function is saying to Python that the variable player1_head is a local variable to the function draw(), and since print("head: ", player1_head) is referencing a local variable before its assignment, the error is shown. You can fix this by using player1_head as a global variable (since you're modifying it, same goes for the variable player1_body, since you're doing player1_body.append(player1_head)), like so:
def draw():
"Advance player and draw game."
global player1_head
#...rest of the code
Note however that you should avoid using global variables when possible, this is one the problems that arises from using them (they can be modified by any function, which can sometimes lead to errors and confusions).

Related

Python UnboundLocalError, what causes it?

In python I wrote:
registered_to = 0
def execute_data():
registered_to += response.text.count("<div class=\"info-msg\">")
But I'm getting:
registered_to += response.text.count("<div class=\"info-msg\">")
UnboundLocalError: local variable 'registered_to' referenced before assignment
Is this what you want?
registered_to = 0
def execute_data():
global registered_to
registered_to += response.text.count("<div class=\"info-msg\">")
global keyword must be used whenever you wish to modify/create global variables from a non-global scope like a function. If you are just using a global variable from a non-global scope and not modifying it, you need not use the keyword.
Examples
Using global variable in a non-global scope but not modifying it
wish = "Hello "
def fun():
print(wish)
fun()
Using global variable in a non-global scope and modifying it as well
wish = "Hello "
def fun():
word += "World"
print(wish)
fun()

Is there any way to import a variable from a function in another file without using return in Python?

I have this pygame code which is divided into multiple files and in one of the files I want to import a variable from a function which is in another file.
# gamescreen.py
highscore = 0
def game_screen(screen):
...
hits = pygame.sprite.spritecollide(player, world_sprites, True, pygame.sprite.collide_mask)
if len(hits) > 0:
if score > highscore:
global highscore
highscore = score
state = PLAYAGAIN
...
return state
# playagain.py
def play_again(screen):
...
highscore_texto = font.render('highscore: {0}'.format(highscore), True, (BLACK))
background.blit(highscore_texto, (30, 25))
...
Is there any way I can acess that highscore variable in the file playagain.py without having to return it in the game_screen function?
When I tried to return it in the game_screen function like this:
return state, highscore
And access it in the playagain.py file like this:
game_screen(screen)[1]
It just ran the game_screen function two times, which is not what I want because it messes up my game.
import gamestream
print(gamestream.highscore)
The variable is set in the module namespace (confusingly named "global" when its really "module global"). Import the module and you've got its namespace.
This doesn't work
from gamestream import highscore
In that case you rebound the current thing in highscore to your module's namespace and it won't see changes to the original. It is the same as if you had done
import gamestream
highscore = gamestream.highscore
A new variable called "highscore" is created in the local module and the current value in gamestream.highscore is bound to it. If gamestream.highscore is reassigned, that wouldn't affect this separate variable.

Python 3: Suspect_ID is not defined

I'm currently working on a town of salem-esc project for a class but I've ran into an issue. I have been trying to make it so that Suspect_ID is available globally but for some reason it is instead saying ("name 'Suspect_ID' is not defined", I'm tried making it outside of the statement with the rest of my global variables to no avail as well. Any suggestions would be helpful, and if any other information is needed feel free to ask, I hope you have better luck with this program than I am currently having.
def readinSuspects():
#local variables
global Suspect_name
Suspect_name=["","","","","","","","","",""]
global Suspect_age
Suspect_age=[0,0,0,0,0,0,0,0,0,0]
global Suspect_motive
Suspect_motive=["","","","","","","","","",""]
global Suspect_ID
Suspect_ID=[0,0,0,0,0,0,0,0,0,0]
global IsMurderer
IsMurderer=[False,False,False,False,False,False,False,False,False,False]
#subprogram body
file = open("suspects.txt","r")
for i in range(0,9):
Suspect_name[i],Suspect_age[i],Suspect_motive[i],
Suspect_ID[i]=file.readline().split(',')
return Suspect_name,Suspect_age,Suspect_motive,Suspect_ID,IsMurderer
edit: I'm now realising the issue may lie elsewhere so following is gonna be the program in it's entirety, it's far from finished and I am aware there are many other bugs etc.
import random
#Global variable
Guesses=[0]
Murderer=[0]
#Read In Function
def readinSuspects():
#local variables
global Suspect_name
Suspect_name=["","","","","","","","","",""]
global Suspect_age
Suspect_age=[0,0,0,0,0,0,0,0,0,0]
global Suspect_motive
Suspect_motive=["","","","","","","","","",""]
global Suspect_ID
Suspect_ID=[0,0,0,0,0,0,0,0,0,0]
global IsMurderer
IsMurderer=[False,False,False,False,False,False,False,False,False,False]
#subprogram body
file = open("suspects.txt","r")
for i in range(0,9):
Suspect_name[i],Suspect_age[i],Suspect_motive[i],
Suspect_ID[i]=file.readline().split(',')
return Suspect_name,Suspect_age,Suspect_motive,Suspect_ID,IsMurderer
#randomly assign murderer
readinSuspects(Suspect_ID)
Murderer = random.randint(0,9)
for i in range(0,9):
if Murderer == i:
#print Suspect_ID if working
print(Suspect_ID[i])
First of all your method readinSuspects() does not take parameters, but you invoke it with one argument - Suspect_ID which is not defined yet.
I have reworked your code so now it must work:
import random
#Global variable
Suspect_name = []
Suspect_age = []
Suspect_motive = []
Suspect_ID = []
IsMurderer = []
Guesses=[0]
Murderer=[0]
#Read In Function
def readinSuspects():
#local variables
global Suspect_name
Suspect_name=["","","","","","","","","",""]
global Suspect_age
Suspect_age=[0,0,0,0,0,0,0,0,0,0]
global Suspect_motive
Suspect_motive=["","","","","","","","","",""]
global Suspect_ID
Suspect_ID=[0,0,0,0,0,0,0,0,0,0]
global IsMurderer
IsMurderer=[False,False,False,False,False,False,False,False,False,False]
#subprogram body
file = open("suspects.txt","r")
for i in range(0,9):
Suspect_name[i],Suspect_age[i],Suspect_motive[i], Suspect_ID[i]=file.readline().split(',')
return Suspect_name,Suspect_age,Suspect_motive,Suspect_ID,IsMurderer
#randomly assign murderer
readinSuspects()
Murderer = random.randint(0,9)
for i in range(0,9):
if Murderer == i:
#print Suspect_ID if working
print(Suspect_ID[i])
Also read about if __name__ == '__main__': - it is a good python practice, but without it it still works. And here you can read how to define global vars in python How to define global variables in python SO
Your code has much more to be discussed, but I will leave your teacher to do it ;)

'draw1' not defined, Highly confused

So I'm making a lottery number drawing machine,
import random
def lottoDraw1():
draw1 = random.randint(1,49)
def lottoDraw2():
draw2 = random.randint(1,49)
if draw2 == draw1:
lottoDraw2()
And I get the error, "NameError: name 'draw1' is not defined"
If I insert:
draw1 = 0
before the code, the answer is always 0.
Even after I define for draw1 to be changed.
What am I doing wrong?
What are Python namespaces all about
This question is asking for namespaces and that is basicially the problem you are having. In lottodraw1 you are only changing the local version of draw1 and thus the global value of draw1 stays unchanged (in your case 0). For that reason you will always use draw1 = None everywhere else.
My approach would be making an array of the draws and having a general draw function:
draws = []
def draw():
new_draw = random.randint(1,49)
if new_draw not in draws:
draws.append(new_draw)
else:
draw()
draw()
draw()
print(draws)
Now you can just call draw and it will add a newly drawn number that does not exist yet.
As noted by Jean-François Fabre the better version would be using sets, which are faster, but only allow unique values:
draws = set()
def draw():
new_draw = random.randint(1,49)
if new_draw not in draws:
draws.add(new_draw)
else:
draw()
draw()
draw()
print(draws)
You need to make draw1 as global variable
draw1, draw2 = None, None
def lottoDraw1():
global draw1
draw1 = random.randint(1,49)
def lottoDraw2():
global draw1
global draw2
draw2 = random.randint(1,49)
if draw2 == draw1:
lottoDraw2()
However, it is not a good approach which is another topic.

Unbound Local Error with global variable [duplicate]

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 7 months ago.
I am trying to figure out why I get an UnboundLocalError in my pygame application, Table Wars. Here is a summary of what happens:
The variables, REDGOLD, REDCOMMAND, BLUEGOLD and BLUECOMMAND, are initialised as global variables:
#Red Stat Section
REDGOLD = 50
REDCOMMAND = 100
#Blue Stat Section
BLUEGOLD = 50
BLUECOMMAND = 100
def main():
[...]
global REDGOLD
global REDCOMMAND
global BLUEGOLD
global BLUECOMMAND
This works when spawning units within the main loop, subtracting funds to spawn units.
Right now, I am trying to set up a system so that when a unit dies, the killer refunds the victim's COMMAND and earns GOLD based on what he killed:
class Red_Infantry(pygame.sprite.Sprite):
def __init__(self, screen):
[...]
self.reward = 15
self.cmdback = 5
[...]
def attack(self):
if self.target is None: return
if self.target.health <= 0:
REDGOLD += self.target.reward #These are the problem lines
BLUECOMMAND += self.target.cmdback #They will cause the UnboundLocalError
#when performed
self.target = None
if not self.cooldown_ready(): return
self.target.health -= self.attack_damage
print "Target's health: %d" % self.target.health
This works right up until the unit dies. Then this happens:
Traceback (most recent call last):
File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 606, in <module>
main()
File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 123, in main
RedTeam.update()
File "C:\Python27\lib\site-packages\pygame\sprite.py", line 399, in update
for s in self.sprites(): s.update(*args)
File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 304, in update
self.attack()
File "C:\Users\Oventoaster\Desktop\Games\Table Wars\Table Wars.py", line 320, in attack
REDGOLD += self.target.reward
UnboundLocalError: local variable 'REDGOLD' referenced before assignment
How do I get the global variables mentioned above to change with the attack block? If it helps, I am using Pygame 2.7.x, so nonlocal won't work :/
global make the global variable visible in the current code block. You only put the global statement in main, not in attack.
ADDENDUM
Here is an illustration of the need to use global more than once. Try this:
RED=1
def main():
global RED
RED += 1
print RED
f()
def f():
#global RED
RED += 1
print RED
main()
You will get the error UnboundLocalError: local variable 'RED' referenced before assignment.
Now uncomment the global statement in f and it will work.
The global declaration is active in a LEXICAL, not a DYNAMIC scope.
You need to declare the variable as global in each scope where they are being modified
Better yet find a way to not use globals. Does it make sense for those to be class attributes for example?
Found that variables in main act like global "read only" variables in function. If we try to reassign the value, it will generate error.
Try:
#!/usr/bin/env python
RED=1
A=[1,2,3,4,5,6]
def f():
print A[RED]
f()
It's ok.
But:
#!/usr/bin/env python
RED=1
A=[1,2,3,4,5,6]
def f():
print A[RED]
A = [1,1,1,1,1]
f()
Generate
File "./test.py", line 6, in f
print A[RED]
UnboundLocalError: local variable **'A'** referenced before assignment
and:
#!/usr/bin/env python
RED=1
A=[1,2,3,4,5,6]
def f():
print A[RED]
RED = 2
f()
Generate
File "./test.py", line 6, in f
print A[RED]
UnboundLocalError: local variable **'RED'** referenced before assignment

Categories

Resources