测试字典中的键

2024-03-29 14:37:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Pyautogui为一个名为“Graal Online Classic”的MMO创建一个bot。它会读取玩家的“PM”信息,然后用适当的回复回复。它还将获取玩家名称的屏幕截图,使用tesseract将其转换为字符串,并将其存储在字典中(同时还存储特定玩家在bot响应层次结构中的位置),并使用该截图确定该玩家之前是否给他们发过消息。你知道吗

def player_id(name_coords, option): # Indentifies person who messaged and depending on if this person has messaged before changes response
    players = {} # Used to store players names and where in the response is
    keys = players.keys()

    image = pyautogui.screenshot('name_test.png', region = name_coords) # Names are screenshots 
    name_image = str(pytesseract.image_to_string(Image.open('name_test.png')))
    print(name_image)
    for name_image in keys:
        print("User is previous user.")
        if players[name_image] == None:
            players[name_image] = option
        else:
            players[name_image] = players[name_image] + option
        return players[name_image]
    else:
        print("User was not previously found, adding to dictionary.")
        players[name_image] = None
        print(players.keys())
        print(players)

问题源于上述函数。当它找到一个以前给他们发过消息的人时,应该转到if语句,但是不管怎样,else语句都将执行。我打印播放器中的值,当播放器第二次向它们发送消息时也是这样,但是对于name\u image in keys:仍然返回false。你知道吗

如果需要,这里是完整的代码https://pastebin.com/haJRfqJD

下面是运行3条消息的程序的日志

None
(454, 222)
Found a waiting message
Found an open message
1003 424
Player messaged 1
1
I hate sand
User was not previously found, adding to dictionary.
dict_keys(['I hate sand'])
{'I hate sand': None}
The bounty quest allows you to hunt mobs for cash! Location: Castle, steward's room(to the right in the throne room) [PM 1 for specifics, PM 2 for TIPS, PM 3 for possible bounties]
Replying...
(454, 222)
Found a waiting message
Found an open message
1003 424
Player messaged 1
1
I hate sand
User was not previously found, adding to dictionary.
dict_keys(['I hate sand'])
{'I hate sand': None}
The bounty quest allows you to hunt mobs for cash! Location: Castle, steward's room(to the right in the throne room) [PM 1 for specifics, PM 2 for TIPS, PM 3 for possible bounties]
Replying...
(454, 222)
Found a waiting message
Found an open message
1003 424
Player messaged 1
1
I hate sand
User was not previously found, adding to dictionary.
dict_keys(['I hate sand'])
{'I hate sand': None}
The bounty quest allows you to hunt mobs for cash! Location: Castle, steward's room(to the right in the throne room) [PM 1 for specifics, PM 2 for TIPS, PM 3 for possible bounties]
Replying...

Tags: thetonameinimagenonemessagefor
1条回答
网友
1楼 · 发布于 2024-03-29 14:37:30

这里不需要for循环。这就是使用字典的全部意义——避免昂贵的O(N)查找。查字典很快。将for循环替换为

name = players.get(name_image)
if name:    
    # your variable naming is totally unclear. I **think** this is what you want
    players[name] = option
    return players[name]
else:
    print("User was not previously found, adding to dictionary.")
    players[name_image] = None
    print(players.keys())
    print(players)

请注意,在原始cdoe中,您定义了一个名称\u映像,如下所示:

name_image = str(pytesseract.image_to_string(Image.open('name_test.png')))

但是变量get在for循环中写得太多了。你知道吗

相关问题 更多 >