2013-09-23 195 views
0

我正在为其中一个受欢迎的MMORPG制作自动化脚本。我收到以下错误:Python名称错误:全局名称'inv'未定义

Traceback (most recent call last): 
    File "<pyshell#9>", line 1, in <module> 
    startFishing() 
    File "C:\Python27\DG\RS\RS bot.py", line 56, in startFishing 
    if inv == "full": 
NameError: global name 'inv' is not defined 

我已在下面详细介绍了我的功能。

def isMyInventoryFull(): 
    s = screenGrab() 
    a = s.getpixel((1173,591)) 
    b = s.getpixel((1222,591)) 
    c = s.getpixel((1271,591)) 
    d = s.getpixel((1320,591)) 
    if a == b == c == d: 
     print "Inventory is full! Time to go back home." 
     inv = "full" 
     print inv 
    else: 
     print "Inventory is not full." 
     inv = "notfull" 
     time.sleep(3) 

def startFishing(): 
    mousePos((530,427)) 
    leftClick() 
    time.sleep(0) 
    inv = 'full' 
    openUpInventory() 
    isMyInventoryFull() 
    if inv == "full": 
     time.sleep(0.01) 
    else: 
     isMyInventoryFull() 
    mousePos((844,420)) 
    rightClick() 
    time.sleep(1) 

的事情是,我有我的“isMyInventoryFull”功能中定义的“INV”,但它不是拿起那INV“已经被定义?我绝对错过了一些东西,任何人都可以帮忙吗?

+0

请解决您的压痕。将代码粘贴到框中,然后突出显示,然后单击“{}”。 – geoffspear

回答

1

该名称inv当前仅在isMyInventoryFull的范围内定义,并且一旦该函数返回,它将停止存在。

我建议你从isMyInventoryFull返回变量inv的价值:

def isMyInventoryFull(): 
    # determine the value of inv 
    return inv 

然后,startFishing可以得到INV的值:

def startFishing(): 
    # ... 
    inv = isMyInventoryFull() 
    # now you can use inv 
0

要么在函数外部定义你的inv变量,要么作为一个全局变量,我认为这将解决你的问题。

相关问题