Global name not defined concept in python
Global name not defined concept in python
No worries 🙂 welcome to Python! Its throwing that error because its looking for a global variable that doesnt exist — and the reason it doesnt exist is because youre not hitting the if type == accounts
condition!
try this:
for i in included:
global signs,accounts, regions
global sign_name, acc_name, rg_name
regions = no region yet
acc_name = no acc_name yet
if type == regions
regions = i
rg_name = regions[data][region]
if type == accounts
accounts = i
acc_name = accounts[data][account]
print(Stopping account + acc_name + in region + rg_name)
That will clear the error and at least let you see what other bugs may be popping up 🙂
Ill also point out, as Im sure you will hear from others, theres no reason for you to be declaring global variables in this context. It was initially saying cant find global variable because before you put in the global
keywords, it wasnt triggering on the if
statement and so first it checked the locals()
variables, and not finding it, searched for the globals()
variables, and not finding it kicked and error.
You can remove the global
variables and it will work fine like so:
for i in included:
regions = no region yet
acc_name = no acc_name yet
if type == regions
regions = i
rg_name = regions[data][region]
if type == accounts
accounts = i
acc_name = accounts[data][account]
print(Stopping account + acc_name + in region + rg_name)
Another quick note, never the type
as a variable… use type_
instead. The reason is type
is a builtin
Python function and if you use type
as a variable you are accidentally aliasing that builtin name.
Finally, just to clean up the script a little more:
# only use i when youre using numbers, otherwise just call it
# the name of the data youre using :)
for account_data in included:
regions = no region yet
acc_name = no acc_name yet
if type_ == regions
rg_name = account_data[data][region]
if type_ == accounts
acc_name = account_data[data][account]
# heres an example of Pythonic string formatting :)
print(Stopping account {} in region {}.format(acc_name, rg_name))
Before you use the global variable acc_name
in your function for reading, it must be first initialized somewhere: either outside of the function or inside it. If type == accounts
, then the variable would be initialized, but if it is not, it wouldnt be.
Global name not defined concept in python
From what I can see is that you never predefine acc_name in both local and global scopes.
The error wouldnt exist if you add acc_name = something
anywhere before print(Stopping account + acc_name + in region + rg_name)
.
The error in your code is that if type == accounts
but the type wasnt accounts so acc_name = accounts[data][account]
never ran.
Try this:
for i in included:
global signs,accounts, regions
global sign_name, acc_name, rg_name
if type == regions:
regions = i
rg_name = regions[data][region]
else:
rg_name=No regions #backup str
if type == accounts:
accounts = i
acc_name = accounts[data][account]
else:
acc_name=No accounts #backup str
print(Stopping account + acc_name + in region + rg_name)
This way there will be no errors
I dont know if that was your code but global
command only need to be used inside functions for that it is not necessary to use it elsewhere.
and as a side note, a little more code would be appreciated and type isnt a good variable because it is also a builtin method.