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

How to set a Variable inside a Function

Sat Oct 15, 2016 8:39 pm

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.

User avatar
DougieLawson
Posts: 40212
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to set a Variable inside a Function

Sun Oct 16, 2016 3:19 pm

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":
print ("Python is virtually unreadable")
else:
print ("Python retains the special syntax of left-hand whitespace")

And

Code: Select all

if code_tags <> "Used":
  print ("Python is virtually unreadable")
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.

Any DMs sent on Twitter will be answered next month.
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

Sun Oct 16, 2016 4:53 pm

edited. thanks

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to set a Variable inside a Function

Sun Oct 16, 2016 5:31 pm

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.

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

Re: How to set a Variable inside a Function

Sun Oct 16, 2016 5:38 pm

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...

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to set a Variable inside a Function

Sun Oct 16, 2016 6:01 pm

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...

Edit to add
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

Sun Oct 16, 2016 6:30 pm

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?

User avatar
bensimmo
Posts: 4988
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to set a Variable inside a Function

Mon Oct 17, 2016 4:45 pm

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

Mon Oct 17, 2016 5:29 pm

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.

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

Re: How to set a Variable inside a Function

Mon Oct 17, 2016 6:40 pm

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.
see also : en.wikipedia.org/wiki/Scope_computer_science#Function_scope

Return to “Beginners”