0

I have stared at this line of code for far too long and I do not understand the exception at all. The line of code is

if '_' in part_word:

and the exception is

Exception has occurred: KeyError
'__________n'
  File "C:\Users\timregan\source\pythonlessons\Hangman-Game\Hangman_learn.py", line 50, in play_and_learn
    if '_' in part_word:
  File "C:\Users\timregan\source\pythonlessons\Hangman-Game\Hangman_learn.py", line 66, in main
    play_and_learn()
  File "C:\Users\timregan\source\pythonlessons\Hangman-Game\Hangman_learn.py", line 71, in <module>
    main()

(N.B. In comments people have asked for more of the code. It is not clear to me which fragment(s) are relevant, so the two Python files are available here, and the problem line is line 50.)

At the point the exception is thrown part_word is '__________n'. Even stranger is this: a watch in the debugging pane clearly shows that '_' in part_word evaluates to True.

VSCode screenshot showing watched expression evaluating to True

The code looks correct—'_' in part_word is the correct way to test if the string part_word contains the string '_'. I can see that it evaluates correctly to True in the debugging pane. Any yet it throws a KeyError in the main code. Why? How do I diagnose what is going wrong? How might I fix this?

8
  • Can you add some part of the code in the question? Commented Oct 30, 2020 at 7:41
  • please provide more code, thx! Commented Oct 30, 2020 at 7:42
  • 2
    If you've got a Hangman_learn.pyc file, delete it. Also, restart Python. Commented Oct 30, 2020 at 7:42
  • I've added a link to the source, here as I am not sure which fragment is relevant (the error is line 50). There is no *.pyc file, and restarting the entire laptop did not fix the issue. I tried changing conda environment too. Commented Oct 30, 2020 at 8:05
  • 1
    No it's ok, accept yours Commented Oct 30, 2020 at 8:32

1 Answer 1

1

It's always the way, soon after asking on SO the answer pops into my head!

This still does not quite make sense, but it looks like the error is on the following line, somehow the debugger and the exception text are reporting the wrong line. This is the problem:

max_future_score = max([score for letter, score in q_table[part_word]])

because of an earlier mistake, part_word is not a key in q_table, hence the KeyError. I'm not sure why it is being reported on the previous line by the exception and the debugger.

(N.B. The answer renders the question wrong, it is not a KeyError with string containment, but I'll leave it up just in case anyone else encounters a KeyError that actually arises from the following statement.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.