Python NameError: глобальное имя «NAME» не определено! (Но он определен)

Это мой код Python:

import es, popuplib, cmdlib info = es.AddonInfo() info['name'] = "bilbodog Vaaben Menu" info['version'] = "1.1" info['author'] = "bilbodog" info['url'] = "" info['description'] = "En vaaben menu der kan give vaaben til spillere" def load(): es.regclientcmd('bm_version', 'bm/bm_version') cmdlib.registerSayCommand('!wpa', sendBmMenu, 'Send BM Menu') es.msg('#multi', '#green[bilbodog Vaaben Menu] #defaultblev loaded!') global bilbodog, bilbodogSelf, bilbodogOther, bilbodogSelf2, bilbodogSelf3, bilbodogSelf4, bilbodogSelf5, bilbodogSelf6, bilbodogSelf7, bilbodogSelf8, bilbodogSelf9, bilbodogOther2, bilbodogOther3, bilbodogOther4, bilbodogOther5, bilbodogOther6, bilbodogOther7, bilbodogOther8, bilbodogOther9, bilbodogadmins bilbodogadmins = ('STEAM_0:1:25150824', 'STEAM_ID_LAN', 'STEAM_ID_PENDING') bilbodog = popuplib.create('bilbodog') bilbodog.addline('---------------bilbodog Vaaben Menu---------------') bilbodog.addline('Weapon Menu - Hvem vil De give et vaaben?') bilbodog.addline('->1. Mig selv') bilbodog.addline('->2. Andre spillere') bilbodog.addline('------------------------------') bilbodog.addline('0 Luk') bilbodog.menuselect = bilbodogscript bilbodogSelf = popuplib.create('bilbodogSelf') bilbodogSelf.addline('---------------bilbodog Vaaben Menu---------------') bilbodogSelf.addline('Weapon Menu - Mig selv') bilbodogSelf.addline('->1. Haandpistoler') bilbodogSelf.addline('->2. Stormgevaerer') bilbodogSelf.addline('->3. Haglgevaerer') bilbodogSelf.addline('->4. Maskinepistoler') bilbodogSelf.addline('->5. Sniper Vaaben') bilbodogSelf.addline('->6. Maskingevaer') bilbodogSelf.addline('->7. Udstyr') bilbodogSelf.addline('->8. Vaaben Saet') bilbodogSelf.addline('------------------------------') bilbodogSelf.addline('0 Luk') bilbodogSelf.menuselect = bilbodogSelfscript bilbodogSelf2 = popuplib.create('bilbodogSelf2') bilbodogSelf2.addline('---------------bilbodog Vaaben Menu---------------') bilbodogSelf2.addline('Weapon Menu - Mig selv - Haandpistoler') bilbodogSelf2.addline('->1.G18') bilbodogSelf2.addline('->2.USP45') bilbodogSelf2.addline('->3.P228') bilbodogSelf2.addline('->4.Desert Eagle') bilbodogSelf2.addline('->5.Five-Seven') bilbodogSelf2.addline('->6.Dual Elite') bilbodogSelf2.addline('------------------------------') bilbodogSelf2.addline('->8.Tilbage') bilbodogSelf2.addline('0.Luk') bilbodogSelf2.menuselect = bilbodogSelf2script # ...SNIP... bilbodogSelf9 = popuplib.create('bilbodogSelf9') bilbodogSelf9.addline('---------------bilbodog Vaaben Menu---------------') bilbodogSelf9.addline('Weapon Menu - Mig selv - Udstyr') bilbodogSelf9.addline('->1.M4A1, Deagle, Nades, Armor') bilbodogSelf9.addline('->2.M4A1, Elite, Nades, Armor') bilbodogSelf9.addline('->3.AK47, Deagle, Nades, Armor') bilbodogSelf9.addline('->4.AK47, Elite, Nades, Armor') bilbodogSelf9.addline('->5.AWP, Deagle, Nades, Armor') bilbodogSelf9.addline('->6.AWP, Elite, Nades, Armor') bilbodogSelf9.addline('------------------------------') bilbodogSelf9.addline('->8.Tilbage') bilbodogSelf9.addline('0.Luk') bilbodogSelf9.menuselect = bilbodogSelf9script bilbodogOther = popuplib.create('bilbodogOther') bilbodogOther.addline('---------------bilbodog Vaaben Menu---------------') bilbodogOther.addline('Weapon Menu - Andre spillere') bilbodogOther.addline('->1. Haandpistoler') bilbodogOther.addline('->2. Stormgevaerer') bilbodogOther.addline('->3. Haglgevaerer') bilbodogOther.addline('->4. Maskinepistoler') bilbodogOther.addline('->5. Sniper Vaaben') bilbodogOther.addline('->6. Maskingevaer') bilbodogOther.addline('->7. Udstyr') bilbodogOther.addline('->8. Vaaben Saet') bilbodogOther.addline('------------------------------') bilbodogOther.addline('0 Luk') bilbodogOther.menuselect = bilbodogOtherscript bilbodogOther2 = popuplib.create('bilbodogOther2') bilbodogOther2.addline('---------------bilbodog Vaaben Menu---------------') bilbodogOther2.addline('Weapon Menu - Andre spillere - Haandpistoler') bilbodogOther2.addline('->1.G18') bilbodogOther2.addline('->2.USP45') bilbodogOther2.addline('->3.P228') bilbodogOther2.addline('->4.Desert Eagle') bilbodogOther2.addline('->5.Five-Seven') bilbodogOther2.addline('->6.Dual Elite') bilbodogOther2.addline('------------------------------') bilbodogOther2.addline('->8.Tilbage') bilbodogOther2.addline('0.Luk') bilbodogOther2.menuselect = bilbodogOther2script # ...SNIP... bilbodogOther9 = popuplib.create('bilbodogOther9') bilbodogOther9.addline('---------------bilbodog Vaaben Menu---------------') bilbodogOther9.addline('Weapon Menu - Andre spillere - Udstyr') bilbodogOther9.addline('->1.M4A1, Deagle, Nades, Armor') bilbodogOther9.addline('->2.M4A1, Elite, Nades, Armor') bilbodogOther9.addline('->3.AK47, Deagle, Nades, Armor') bilbodogOther9.addline('->4.AK47, Elite, Nades, Armor') bilbodogOther9.addline('->5.AWP, Deagle, Nades, Armor') bilbodogOther9.addline('->6.AWP, Elite, Nades, Armor') bilbodogOther9.addline('------------------------------') bilbodogOther9.addline('->8.Tilbage') bilbodogOther9.addline('0.Luk') bilbodogOther9.menuselect = bilbodogOther9script def sendBmMenu(userid, args): steam = es.getplayersteamid(userid) if steam in bilbodogadmins: bilbodog.send(userid) else: es.tell(event_var['userid'], '#multi', '#greenDe har ikke adgang til denne kommando!') def bilbodogscript(userid, choice, popupid): if choice == 1: bilbodogSelf.send(userid) elif choice == 2: bilbodogOther.send(userid) def bilbodogSelfscript(userid, choice, popupid): if choice == 1: bilbodogSelf2.send(userid) elif choice == 2: bilbodogSelf3.send(userid) elif choice == 3: bilbodogSelf4.send(userid) elif choice == 4: bilbodogSelf5.send(userid) elif choice == 5: bilbodogSelf6.send(userid) elif choice == 6: bilbodogSelf7.send(userid) elif choice == 7: bilbodogSelf8.send(userid) elif choice == 8: bilbodogSelf9.send(userid) def bilbodogOtherscript(userid, choice, popupid): if choice == 1: bilbodogOther2.send(userid) elif choice == 2: bilbodogOther3.send(userid) elif choice == 3: bilbodogOther4.send(userid) elif choice == 4: bilbodogOther5.send(userid) elif choice == 5: bilbodogOther6.send(userid) elif choice == 6: bilbodogOther7.send(userid) elif choice == 7: bilbodogOther8.send(userid) elif choice == 8: bilbodogOther9.send(userid) def bm_version(): es.msg('#multi', '#green[bilbodog Vaaben Menu] #defaultVersion 1.0') es.msg('#multi', '#green[bilbodog Vaaben Menu] #defaultLavet af bilbodog') def unload(): cmdlib.unregisterSayCommand('!wpa') 

Я получаю сообщение об ошибке:

 NameError: global name 'bilbodogOther' is not not defined 

И ошибка происходит прямо здесь:

 def bilbodogscript(userid, choice, popupid): if choice == 1: bilbodogSelf.send(userid) elif choice == 2: bilbodogOther.send(userid) 

Но я не понимаю, что это значит?

Когда вы выполните:

 global bilbodog print bilbodog 

У вас есть эта ошибка:

 Traceback (most recent call last): File "D:\Documents and Settings\My Documents\StackOverflow\test.py", line 2, in <module> print bilbodog NameError: global name 'bilbodog' is not defined 

Вместо этого, когда вы правильно определяете свои переменные:

 global bilbodog bilbodog = 15 #Here you give a value to bilbodog (even None) print bilbodog 

Это работает !!!

Но если вы хотите сделать это с помощью pythonic:

 def definition(): global bilbodog bilbodog = 15 #Here you give a value to bilbodog (even None) def test(): print bilbodog if __name__ == '__main__': definition() test() 

Итак, когда вы делаете bilbodog.menuselect = bilbodogscript я думаю, вы пытаетесь выполнить bilbodogscript() тогда как bilbodog еще не определен. Проблема в том, что с этой строкой вы должны сначала получить эту ошибку:

 Traceback (most recent call last): File "D:\Documents and Settings\My Documents\StackOverflow\test.py", line 3, in <module> bilbodog.menuselect = bilbodogscript NameError: name 'bilbodogscript' is not defined 

В Python «глобальный» означает «уровень модуля», и его использование используется только для имен модулей, которые необходимо отсканировать внутри функции. Это не означает, что «декларация вперед» на уровне модуля – то, чего не существует, поскольку, когда модуль загружается (выполняется или импортируется), весь код на верхнем уровне выполняется последовательно.

 global variable variable = 100 #Any value required def function1(): global variable variable = new_value # manipulation def function2(): print variable # accessed 

Извините, ребята, но для меня код, сообщенный VGO, не работает:

 def f(): global a a = 0 def b(): print(a) b() 

У меня всегда есть NameError:

глобальное имя «a» не определяется ошибкой. Я запускаю сценарий с машины W10 и python 2.7.12

Interesting Posts