Answers.Body: <p>As per: <a href="https://python-chess.readthedocs.io/en/latest/engine.html#indefinite-or-infinite-analysis" rel="nofollow noreferrer">https://python-chess.readthedocs.io/en/latest/engine.html#indefinite-or-infinite-analysis</a></p>
  
  <p><code>engine.analysis(board)</code> now returns an <code>AnalysisResult</code> object, which has the property <code>info</code> that you want.</p>
  
  <p>So instead of:</p>
  
  <pre><code>nwinfo = engine.analysis(...)
  print(nwinfo["score"])
  </code></pre>
  
  <p>You would loop over <code>info</code> in the <code>AnalysisResult</code>, and print the score property from each element:</p>
  
  <pre><code>with engine.analysis(...) as analysis:
      for info in analysis:
          print(info.get("score"))
  </code></pre>
  
  <p>However, your use case looks like it may benefit from <a href="https://python-chess.readthedocs.io/en/latest/engine.html#analysing-and-evaluating-a-position" rel="nofollow noreferrer"><code>engine.analyse</code></a> more than <code>engine.analysis</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-07T14:25:07.547Z
Answers.Text: thanks now the error gones but i getting a hundreds of outputs
Answers.UserDisplayName: shar
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-07T14:32:12.340Z
Answers.Text: This is because `engine.analysis` gives an indefinite analysis - i.e. the engine will keep analyzing forever. You can resolve this by setting some termination condition and exiting the loop, or you could use `engine.analyse` instead, which allows you to set a limit beforehand. Check the documentation link in my answer.
Answers.UserDisplayName: konsolas
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-07T14:46:40.987Z
Answers.Text: every time i run my script i getting a different score are for the same moves are that normal ?
Answers.UserDisplayName: shar
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-07T14:58:44.827Z
Answers.Text: Engines aren't wholly deterministic. As long as the scores are reasonable each time, it's probably correct.
Answers.UserDisplayName: konsolas
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-07T14:15:38.273Z
Answers.LastActivityDate: 2019-08-07T14:33:14.920Z
Answers.LastEditDate: 2019-08-07T14:33:14.920Z
Answers.UserDisplayName: konsolas
Body: <p>I am working on a simple cheat detection script to find if a user used one of the  top engine moves in a game, so I found this great python-chess library
  Additionally, <a href="https://chess.stackexchange.com/questions/15723/returning-scores-for-all-possible-moves-from-a-chess-engine/15734#comment39098_15734">I found this example</a> to getting the moves score. But that topic is an old version of the lib so I tried to update the script. However, I keep getting errors, and I don't know how to get the scores in the right way.</p>
  
  <pre><code>import asyncio
  import chess
  #import chess.uci
  import chess.engine
  import chess.pgn
  import io
  #Let's try our code with the starting position of chess:
  
  fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
  board = chess.Board(fen)
  
  
  #Now make sure you give the correct location for your stockfish engine file
  #...in the line follows: e.g., /home/.../stockfish_6_x64
  
  
  engine = chess.engine.SimpleEngine.popen_uci('/home/shar/Desktop/cheat detect python/stockfish-10-linux/Linux/stockfish_10_x64')
  
  if board.turn: print( 'White to move');
  else: print( 'black to move');
  
  for el in board.legal_moves:
  
  
  
      pgn = io.StringIO(board.san(el))
  
      thnwpgntofen = chess.pgn.read_game(pgn);
      nwinfo = engine.analysis(thnwpgntofen.board());
  
  
  
      print(nwinfo["score"])
  
  
      print("-------------------------");
  </code></pre>
  
  <p>I get this error after running my code:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):   File "prntengmv.py", line 37, in
    
        print(nwinfo["score"]) TypeError: 'SimpleAnalysisResult' object is not subscriptable</p>
  </blockquote>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-07T04:37:24.783Z
Comments.Text: The updated version is added [here](https://chess.stackexchange.com/a/15734/3594).
Comments.UserDisplayName: Ellie
Tags: <engines><programming><python>
Title: Python-chess : how to get a list for the best engine moves
ThreadText: # Python-chess : how to get a list for the best engine moves
  
  (Asked by: username_0 on 2019-08-07)
  
  I am working on a simple cheat detection script to find if a user used one of the  top engine moves in a game, so I found this great python-chess library
  Additionally, I found this example to getting the moves score. But that topic is an old version of the lib so I tried to update the script. However, I keep getting errors, and I don't know how to get the scores in the right way.
  import asyncio
  import chess
  #import chess.uci
  import chess.engine
  import chess.pgn
  import io
  #Let's try our code with the starting position of chess:
  
  fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
  board = chess.Board(fen)
  
  
  #Now make sure you give the correct location for your stockfish engine file
  #...in the line follows: e.g., /home/.../stockfish_6_x64
  
  
  engine = chess.engine.SimpleEngine.popen_uci('/home/username_0/Desktop/cheat detect python/stockfish-10-linux/Linux/stockfish_10_x64')
  
  if board.turn: print( 'White to move');
  else: print( 'black to move');
  
  for el in board.legal_moves:
  
  
  
      pgn = io.StringIO(board.san(el))
  
      thnwpgntofen = chess.pgn.read_game(pgn);
      nwinfo = engine.analysis(thnwpgntofen.board());
  
  
  
      print(nwinfo["score"])
  
  
      print("-------------------------");
  
  I get this error after running my code:
  
  Traceback (most recent call last):   File "prntengmv.py", line 37, in
    
        print(nwinfo["score"]) TypeError: 'SimpleAnalysisResult' object is not subscriptable
  
  **Tags:** engines, programming, python
  
  > **Comments:**
  > **username_1**: The updated version is added [here](https://chess.stackexchange.com/a/15734/3594).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-08-07. Score: 4 | Accepted answer
  
  As per: https://python-chess.readthedocs.io/en/latest/engine.html#indefinite-or-infinite-analysis
  engine.analysis(board) now returns an AnalysisResult object, which has the property info that you want.
  So instead of:
  nwinfo = engine.analysis(...)
  print(nwinfo["score"])
  
  You would loop over info in the AnalysisResult, and print the score property from each element:
  with engine.analysis(...) as analysis:
      for info in analysis:
          print(info.get("score"))
  
  However, your use case looks like it may benefit from engine.analyse more than engine.analysis.
  
  > **Comments:**
  > **username_0**: thanks now the error gones but i getting a hundreds of outputs  
  > **username_2**: This is because `engine.analysis` gives an indefinite analysis - i.e. the engine will keep analyzing forever. You can resolve this by setting some termination condition and exiting the loop, or you could use `engine.analyse` instead, which allows you to set a limit beforehand. Check the documentation link in my answer.  
  > **username_0**: every time i run my script i getting a different score are for the same moves are that normal ?  
  > **username_2**: Engines aren't wholly deterministic. As long as the scores are reasonable each time, it's probably correct.  
  
  ---
  
  