global name is not defined python function
Python NameError: el nombre global ''NAME'' no está definido!(Pero está definido) (3)
Este es mi código 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'')
Me sale un error:
NameError: global name ''bilbodogOther'' is not not defined
Y el error está ocurriendo aquí:
def bilbodogscript(userid, choice, popupid):
if choice == 1:
bilbodogSelf.send(userid)
elif choice == 2:
bilbodogOther.send(userid)
Pero no lo entiendo, ¿qué significa?
Cuando tu lo hagas:
global bilbodog
print bilbodog
Tienes este error:
Traceback (most recent call last):
File "D:/Documents and Settings/My Documents//test.py", line 2, in <module>
print bilbodog
NameError: global name ''bilbodog'' is not defined
En su lugar cuando define sus variables correctamente:
global bilbodog
bilbodog = 15 #Here you give a value to bilbodog (even None)
print bilbodog
Funciona !!!
Pero si quieres hacerlo de forma pitónica:
def definition():
global bilbodog
bilbodog = 15 #Here you give a value to bilbodog (even None)
def test():
print bilbodog
if __name__ == ''__main__'':
definition()
test()
Entonces, cuando haces bilbodog.menuselect = bilbodogscript
, creo que intentas ejecutar bilbodogscript()
mientras que bilbodog
aún no está definido. El problema es que con esta línea primero debes tener este error:
Traceback (most recent call last):
File "D:/Documents and Settings/My Documents//test.py", line 3, in <module>
bilbodog.menuselect = bilbodogscript
NameError: name ''bilbodogscript'' is not defined
En Python, "global" significa "nivel de módulo", y su uso solo está dentro de la función para los nombres de nivel de módulo que deben rebotar dentro de la función. No se entiende como una "declaración hacia adelante" de nivel de módulo, algo que no existe, ya que cuando un módulo se carga (ejecuta o importa), todo el código en el nivel superior se ejecuta de manera secuencial.
global variable
variable = 100 #Any value required
def function1():
global variable
variable = new_value # manipulation
def function2():
print variable # accessed