Flaw in hangman program
<p>I need to write a simple hangman function that takes in one string (the word that's being guessed) and a list of letters (the letters that are guessed).
Here is my code so far:</p>

<pre><code>def WordGuessed(Word, letters):
    if letters == []:
        return False
    else:
        for i in letters:
            if i not in Word:
                return False
        else:
            if i == letters[-1]:
                return True
</code></pre>

<p>The code usually works, but I am finding that occasionally prints the wrong answer. For example, if </p>

<pre><code>WordGuessed('durian', ['h', 'a', 'c', 'd', 'i', 'm', 'n', 'r', 't', 'u'])
</code></pre>

<p>It prints False when it should be printing True. Can anyone see where my code is wrong?</p>

<p>You are returning False as soon as you find a guessed letter that is not in the word. In your example, the very first letter is not in the word.</p>

<p>It would work if instead you loop through <code>Word</code> and check each letter if it is in the array <code>letters</code>:</p>

<pre><code>def WordGuessed(Word, letters):
    if letters == []:
        return False
    else:
        for i in Word:
            if i not in letters:
                return False
        else:
            if i == Word[-1]:
                return True

# prints True
print(WordGuessed('durian', ['h', 'a', 'c', 'd', 'i', 'm', 'n', 'r', 't', 'u']))

# prints False, missing 'u'
print(WordGuessed('durian', ['h', 'a', 'c', 'd', 'i', 'm', 'n', 'r', 't']))
</code></pre>

python