这是我的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)
但我不明白,这是什么意思?
答案 0 :(得分:26)
当你这样做时:
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
答案 1 :(得分:5)
在Python中,“全局”表示“模块级别”,并且它仅在函数内用于要在函数内反弹的模块级别名称。它不是一个模块级别的“前向声明” - 不存在的东西,因为当加载(执行或导入)模块时,顶层的所有代码都是按顺序执行的。
答案 2 :(得分:4)
global variable
variable = 100 #Any value required
def function1():
global variable
variable = new_value # manipulation
def function2():
print variable # accessed