I am writing an app, and I need to set the background of the elements correspondingly, so I wrote this little code that makes a variable for every language background. It works perfectly when run in main (without any modifications), but like a function, it doesn't! Here is the code I'm talking about:
available_languages = ["english", "german", "spanish", "ukrainian", "russian", "italian",
"portuguese", "french", "arabic", "mandarin", "japanese", "hindi"]
current = "english"
for current_language in available_languages:
if current == current_language:
exec(current_language + "bg = [.7, .7, .7, 1]")
print(englishbg)
else:
exec(current_language + "bg = [0, 0, 0, 0]")
print(englishbg)
Now when I run it as a function like this:
def getlanguagelists():
available_languages = ["english", "german", "spanish", "ukrainian", "russian", "italian",
"portuguese", "french", "arabic", "mandarin", "japanese", "hindi"]
current = "english"
for current_language in available_languages:
if current == current_language:
exec(current_language + "bg = [.7, .7, .7, 1]")
print(englishbg)
else:
exec(current_language + "bg = [0, 0, 0, 0]")
print(englishbg)
getlanguagelists()
I get this exception:
Traceback (most recent call last):
File "C:\Users\siuba\Desktop\t.py", line 17, in <module>
getlanguagelists()
File "C:\Users\siuba\Desktop\t.py", line 10, in getlanguagelists
print(englishbg)
NameError: name 'englishbg' is not defined
Why could this happen?