0

我正在为一个流行的 MMORPG 制作一个自动化脚本。我收到以下错误:

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”已经被定义?我肯定在这里遗漏了一些东西,有人可以帮忙吗?

4

2 回答 2

1

名称 inv 目前仅在 的范围内定义isMyInventoryFull,一旦函数返回,它将不再存在。

我建议您从以下位置返回变量的invisMyInventoryFull

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

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

def startFishing():
    # ...
    inv = isMyInventoryFull()
    # now you can use inv
于 2013-09-23T12:13:49.467 回答
0

在函数之外定义您的 inv 变量,或者作为全局变量,我认为这将解决您的问题。

于 2013-09-23T11:58:06.513 回答