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()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 assignmentthanks in advance!