for loop iterates over first letter only
<p>My loop seems to iterate over the first letter and then breaks though it's supposed to iterate through each letter in the secretWord, for example, the code bellow is supposed to print out "_pp_e" but instead it only prints "_". I don't understand, what's the problem with that code??</p>

<pre><code>def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''
    # FILL IN YOUR CODE HERE...
    for letter in secretWord:
        if letter in lettersGuessed:
            return letter
        else:
            return '_'

print(getGuessedWord("apple",  ['e', 'i', 'k', 'p', 'r', 's']))
</code></pre>

<p>You <code>return</code> from the function in the first iteration. <code>return</code> <strong>ends</strong> a function, there and then, so the <code>for</code> loop won't continue either.</p>

<p>You need to build up your return value <em>in the function itself</em>. Build up the resulting string one character at a time, by using a list to hold all the characters first then joining those together into one string at the end:</p>

<pre><code>def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''
    guessed = []
    for letter in secretWord:
        if letter in lettersGuessed:
            guessed.append(letter)
        else:
            guessed.append('_')
    return ''.join(guessed)
</code></pre>

<p>If you are feeling adventurous, you could even make that a <a href="https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehension</a> and do all the work in one line:</p>

<pre><code>def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''
    return ''.join([l if l in lettersGuessed else '_' for l in secretWord])
</code></pre>

<p>Either version produces the expected output:</p>

<pre><code>&gt;&gt;&gt; print(getGuessedWord("apple",  ['e', 'i', 'k', 'p', 'r', 's']))
_pp_e
</code></pre>

python debugging for-loop