ground_
Posts: 17
Joined: Tue Oct 14, 2014 6:13 am

python unboundlocalerror

Fri Oct 24, 2014 10:02 pm

Hello all,

last week I red a book about writing in python and did some tutorials. Now i am getting the hang of it, and I wanted to write a program which guide me through some steps in order to turn on my LED light. But now i am experiencing an error which I can't figure out (but seems quite simple):

I wrote the following code:

Code: Select all

def led():
    import RPi.GPIO as GPIO
    import os
    print ("please input which mode to use for GPIO pin Numberering: \n")
    print ("1 = BOARD")
    print ("2 = BCM")
    print ("\n")
    smode = input("please enter you choice: ")

    if smode == "1":
        mode="BOARD"
    elif smode == "2":
        mode="BCM"
    else:
        print ("no valid choice, please select '1' or '2'")
        os.system("clear")
        led()

    print("selected: >>> ", mode)
    input()
now this is my error:
the first run goes ok, if i select 1 or 2 it will show me which mode i selected, and goes on with the script after i press enter.

But when i select something else then 1 or 2, it will start from the beginning of the function (so it is still doing what I want). When i select 1 or 2 now, it will still print the ("selected: >>>", mode) fine, but also gives an error and closes the program (instead of going on with the script).

the error is as following:

Code: Select all

File "options.py", line 44, in led
    print("selected: >>> ", mode)
UnboundLocalError: local variable 'mode' referenced before assignment
now i cant figure out why it is doing this, can anybody here explain it maybe?

thanks in advance!

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

Re: python unboundlocalerror

Fri Oct 24, 2014 11:33 pm

Code: Select all

    if smode == "1":
        mode="BOARD"
    elif smode == "2":
        mode="BCM"
    else:
        print ("no valid choice, please select '1' or '2'")
        os.system("clear")
        led()
What happens if smode != "1" AND smode != "2" you don't set a default value for mode.

Code: Select all

    if smode == "1":
        mode="BOARD"
    elif smode == "2":
        mode="BCM"
    else:
        print ("no valid choice, please select '1' or '2'")
        mode = "INVALID"  # set a bad value when smode isn't 1 or 2
        os.system("clear")
        led() 
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

ground_
Posts: 17
Joined: Tue Oct 14, 2014 6:13 am

Re: python unboundlocalerror

Sat Oct 25, 2014 2:32 am

well, i thought it would go to the else function then (and restart at the beginning), but defining it would be a good idea.

well i think I (accidentally) figured it out:
I simply put a return at the end of the ELSE: block. Other wise the functions keeps running while it started again (and 2 are running at the same time).

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: python unboundlocalerror

Sat Oct 25, 2014 8:59 am

Another way to do it, without calling the led() function again would be as follows:

Code: Select all

smode = None

while not smode in ["1", "2"]:

    smode = input("please enter you choice: ")

    if smode == "1":
        mode="BOARD"
    elif smode == "2":
        mode="BCM"
    else:
        print ("no valid choice, please select '1' or '2'")
        os.system("clear")
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”