【发布时间】:2017-05-29 15:27:17
【问题描述】:
我正在尝试使用 Pyautogui 为名为“Graal Online Classic”的 MMO 创建一个机器人。它会读取向他们发送消息的玩家的“PM”,然后以适当的响应进行回复。它还将截取玩家姓名的屏幕截图,将其转换为带有 tesseract 的字符串,并将其存储在字典中(并且还存储该特定玩家在机器人的响应层次结构中的位置)并使用它来确定玩家是否已发送消息他们之前。
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_image 中的键:仍然返回 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...
【问题讨论】:
-
如果您要检查密钥是否存在,请使用
if not name_image in players:。 -
name_image取自players.keys()。在什么情况下与None的值挂钩?
标签: python python-3.x dictionary