2017-05-29 9 views
0

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文が代わりに実行されます。プレイヤーが2回目のメッセージを送ってもキーの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... 
+0

キーが存在するかどうかを確認している場合は、 'playerでname_imageではない場合:'を使用します。 – Darkstarone

+0

'name_image'は' players.keys() 'から取られます。どのような状況下では、値「None」にリンクされていますか? – khelwood

+0

http://stackoverflow.com/help/someone-answers – e4c5

答えて

1

のために動作しているプログラムのログです。これは、高価なO(N)検索を避けるために、辞書を使用する全体のポイントです。辞書検索は高速です。

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に、あなたはname_imageを定義していることを注意してループのためにあなたを交換してください:forループで書かれた上で

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

しかし、その変数はのを取得します。

+0

ありがとう、あなたの助けをありがとうが、名前:ちょうど実行したくない場合。ここに、同じ名前の3つのメッセージを自分自身で送信したときのログがあります。 CANT Find DIGIT ANSWER私は砂が嫌いです ユーザーは以前には見つかりませんでした。 dict_keys(['私は砂が嫌い]) {'私は砂が嫌い:なし} なし 私は砂が嫌い ユーザーは以前には見つかりませんでした。 dict_keys(['私は砂を嫌う]) {'私は砂を ':なし}実際には文字の制限のためにログ全体を投稿できませんが、上記からわかるようにif文は実行されませんその値は辞書になければなりません。 –

+0

私は早すぎると答えました、もう一度チェックしてください。 –

+0

メイン・ポストにログを追加しました。 –