Python NameError:全局名称“name”不是没有定义的!(但它是定义)

时间:2021-10-17 18:09:40

This is my Python code:

这是我的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')

I get an error:

我得到一个错误:

NameError: global name 'bilbodogOther' is not not defined

And the error is going on right here:

误差在这里

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

But I don't understand it, what does it mean?

但我不明白,这是什么意思?

3 个解决方案

#1


24  

When you do:

当你做的事:

global  bilbodog
print bilbodog

You have this 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

Instead when you define your variables correctly:

相反,当你正确地定义你的变量时:

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

It does work !!!

它确实工作! ! !

But if you want to do it in a pythonic way:

但是如果你想用python的方式来做:

def definition():
    global bilbodog
    bilbodog = 15 #Here you give a value to bilbodog (even None)

def test():
    print bilbodog

if __name__ == '__main__':
    definition()
    test()

So, when you do bilbodog.menuselect = bilbodogscript I think you try to execute bilbodogscript() whereas bilbodog is not yet defined. The problem is that with this line you should first have this error:

所以,当你做bilbodog的时候。我认为您试图执行bilbodogscript(),而bilbodog还没有定义。问题是,在这条直线上,你首先应该有这个错误:

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

#2


4  

In Python, "global" means "module level", and it's only use is within function for module level names that are to be rebound within the function. It's not meant as a module level "forward declaration" - something that doesn't exist, since when a module is loaded (executed or imported), all code at the top level is executed sequentially.

在Python中,“全局”的意思是“模块级别”,它只在模块级名称的函数内使用,这些名称在函数中是反弹的。它不是一个模块级的“前向声明”——这是不存在的,因为当一个模块被加载(执行或导入)时,顶层的所有代码都是按顺序执行的。

#3


2  

global  variable 
variable = 100 #Any value required 

def function1():
    global variable
    variable = new_value # manipulation 

def function2():
    print variable # accessed

#1


24  

When you do:

当你做的事:

global  bilbodog
print bilbodog

You have this 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

Instead when you define your variables correctly:

相反,当你正确地定义你的变量时:

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

It does work !!!

它确实工作! ! !

But if you want to do it in a pythonic way:

但是如果你想用python的方式来做:

def definition():
    global bilbodog
    bilbodog = 15 #Here you give a value to bilbodog (even None)

def test():
    print bilbodog

if __name__ == '__main__':
    definition()
    test()

So, when you do bilbodog.menuselect = bilbodogscript I think you try to execute bilbodogscript() whereas bilbodog is not yet defined. The problem is that with this line you should first have this error:

所以,当你做bilbodog的时候。我认为您试图执行bilbodogscript(),而bilbodog还没有定义。问题是,在这条直线上,你首先应该有这个错误:

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

#2


4  

In Python, "global" means "module level", and it's only use is within function for module level names that are to be rebound within the function. It's not meant as a module level "forward declaration" - something that doesn't exist, since when a module is loaded (executed or imported), all code at the top level is executed sequentially.

在Python中,“全局”的意思是“模块级别”,它只在模块级名称的函数内使用,这些名称在函数中是反弹的。它不是一个模块级的“前向声明”——这是不存在的,因为当一个模块被加载(执行或导入)时,顶层的所有代码都是按顺序执行的。

#3


2  

global  variable 
variable = 100 #Any value required 

def function1():
    global variable
    variable = new_value # manipulation 

def function2():
    print variable # accessed