How to set a Variable inside a Function

ScarletSwitch
Posts: 6
Joined: Sat Oct 15, 2016 8:28 pm

How to set a Variable inside a Function

So I set a variable inside a function, but now the other functions don't know what the variable equals
My code is in a sense hat so sorry if it doesn't make sense

Code: Select all

``````from random import randint
from sense_hat import SenseHat
from time import sleep
sense = SenseHat()
start = 0
if start == 0:
sense.clear()
start + 1
def y_integer():
y = 1
def random_integer():
randy = randint(1,7)
print (randy)
def obstacle():
y = y + 1
if randy == 1:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
sense.set_pixel(randy + 4,y,0,255,0)
sense.set_pixel(randy + 5,y,0,255,0)
sense.set_pixel(randy + 6,y,0,255,0)
elif randy == 2:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
sense.set_pixel(randy + 4,y,0,255,0)
sense.set_pixel(randy + 5,y,0,255,0)
elif randy == 3:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
sense.set_pixel(randy + 4,y,0,255,0)
elif randy == 4:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
elif randy == 5:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
elif randy == 6:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
elif randy == 7:
sense.set_pixel(randy,y,0,255,0)
obstacle()
sleep(1)
print ("this means it's working")
``````
ERROR MESSAGE
UnboundLocalError: local variable 'y' referenced before assignment on line 51 in main.py
Last edited by ScarletSwitch on Sun Oct 16, 2016 4:53 pm, edited 1 time in total.

DougieLawson
Posts: 40247
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

Re: How to set a Variable inside a Function

If you're going to post python code on the forum can you put it in [code]... your python goes here ...[/code] tags.

That makes the difference between

if code_tags <> "Used":
else:
print ("Python retains the special syntax of left-hand whitespace")

And

Code: Select all

``````if code_tags <> "Used":
else:
print ("Python retains the special syntax of left-hand whitespace")``````
You may want to edit your post.
Criticising any questions is banned on this forum.

All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

ScarletSwitch
Posts: 6
Joined: Sat Oct 15, 2016 8:28 pm

Re: How to set a Variable inside a Function

edited. thanks

bensimmo
Posts: 4994
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to set a Variable inside a Function

The function is never called and the function never returns it value, so it's only seen in that function.
Just set the variable to a number in the function you use it in at the start on its own if you need to.

EDITING as now I can test in https://trinket.io/sense-hat

basically what you are doing and the error is saying is y is a local variable (it's only seen and used in that one function).
BUT you are trying to tell it y=y+1, unfortunately y doesn't equal anything yet.

B.Goode
Posts: 10727
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: How to set a Variable inside a Function

Same answer written a different way.

The first thing you do in the main program is call obstacle()

The first thing obstacle() does is y = y + 1

What is the value of y that you want to add 1 to? We don't know.

That's what the error message says...

bensimmo
Posts: 4994
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to set a Variable inside a Function

Code: Select all

``````from random import randint
from sense_hat import SenseHat
from time import sleep

sense = SenseHat()

#Set variables
y = 0  #SenseHat LED Row
start = 0

def obstacle(y):  #tell it to expect y as a variable

if randy == 1:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
sense.set_pixel(randy + 4,y,0,255,0)
sense.set_pixel(randy + 5,y,0,255,0)
sense.set_pixel(randy + 6,y,0,255,0)
elif randy == 2:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
sense.set_pixel(randy + 4,y,0,255,0)
sense.set_pixel(randy + 5,y,0,255,0)
elif randy == 3:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
sense.set_pixel(randy + 4,y,0,255,0)
elif randy == 4:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
elif randy == 5:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
elif randy == 6:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
elif randy == 7:
sense.set_pixel(randy,y,0,255,0)

#while true so that it actually loops, else it would only do it once.
while True:
if start == 0:  # See if first run, if so make sure LED is clear, (could just run it before the while True:)
sense.clear()
start + 1

randy = randint(1,7)  #set x column on hat
y=y+1 #increment y, this will soon error out the LED
print (randy)
print (y)

obstacle(y)  #Call obstacle function, telling it what y is equal to
sleep(1)
print ("this means it's working")``````

Have a read through that see if you follow my crude code and comments. y will error out after a bit though and there are a few error left in...

https://trinket.io/python/5b068c4c6a

ScarletSwitch
Posts: 6
Joined: Sat Oct 15, 2016 8:28 pm

Re: How to set a Variable inside a Function

bensimmo wrote:

Code: Select all

``````#while true so that it actually loops, else it would only do it once.
while True:

randy = randint(1,7)  #set x column on hat
y=y+1 #increment y, this will soon error out the LED
obstacle(y)  #Call obstacle function, telling it what y is equal to
sleep(1)
print ("this means it's working")``````
This way it changes randy after every time it changes y. Any advice on how I can make randy stay the same while Y keeps adding 1 until it's off the SenseHat?

bensimmo
Posts: 4994
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to set a Variable inside a Function

Put it before the whole loop.

ewaller
Posts: 32
Joined: Tue Oct 04, 2016 5:24 pm

Re: How to set a Variable inside a Function

There are a couple ways to do what you need. The most Pythonic would be to organize the functions that need access to the variable into a class, then make it a class variable.

The more intuitive route for those learning python would be to declare the variable at the beginning of the program outside of any functions. Then, if a function needs to set the variable, declare the variable as global inside the function. Functions that only need to read the variable do not need (and should not have) the global. declaration.

Code: Select all

``````from random import randint
from sense_hat import SenseHat
from time import sleep
sense = SenseHat()
start = 0
y=None
randy=None

if start == 0:
sense.clear()
start + 1
def y_integer():
global y
y = 1
def random_integer():
global randy
randy = randint(1,7)
print (randy)
def obstacle():
global y
y = y + 1
if randy == 1:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
sense.set_pixel(randy + 4,y,0,255,0)
sense.set_pixel(randy + 5,y,0,255,0)
sense.set_pixel(randy + 6,y,0,255,0)
elif randy == 2:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
sense.set_pixel(randy + 4,y,0,255,0)
sense.set_pixel(randy + 5,y,0,255,0)
elif randy == 3:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
sense.set_pixel(randy + 4,y,0,255,0)
elif randy == 4:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
sense.set_pixel(randy + 3,y,0,255,0)
elif randy == 5:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
sense.set_pixel(randy + 2,y,0,255,0)
elif randy == 6:
sense.set_pixel(randy,y,0,255,0)
sense.set_pixel(randy + 1,y,0,255,0)
elif randy == 7:
sense.set_pixel(randy,y,0,255,0)
obstacle()
sleep(1)
print ("this means it's working")``````
Note, for example, I did not declare raandy as global in obstacle. As it is not defined in the context, it Python looks to the global space. If one does assign a value to a variable in a function, the global is required, otherwise Python assumes it is a local variable that overrides the global space.
Last edited by ewaller on Mon Oct 17, 2016 8:15 pm, edited 1 time in total.

r3d4
Posts: 984
Joined: Sat Jul 30, 2011 8:21 am
Location: ./

Re: How to set a Variable inside a Function

bensimmo wrote:The function is never called and the function never returns it value, so it's only seen in that function.
Just set the variable to a number in the function you use it in at the start on its own if you need to.