Cannot make boolean global in Python
<p>I am very new to programming and am creating a function that takes a letter or letters and checks to see if it is within a word. I need to control the number of guesses so I would like to set a boolean to test whether it was correct. To be clear, I have looked through many other answers on this type of question but cannot figure out what is going on in this case.</p>

<p>I cannot seem to make the bool 'correct' return True even if the if statement is true. After looking this up it seems that making it a global variable should fix this, but it is not working for me. It's still returning False. 
How do I fix this? I am using Python 2.5.</p>

<p>Any help would be greatly appreciated!
Thanks! </p>

<pre><code>randomWord = choose_word(wordlist)
guessedLetters = []   
correct = False
def userGuesses(letters):       
    newLetters = list(letters)          

    global correct      #change correct to global

    for i in range(len(letters)):   
        curLetter = letters[i]

        for j in range(len(randomWord)):    

            if curLetter == randomWord[j]: 
                guessedLetters[j] = randomWord[j]
                correct = True              

            else: 


                correct = False



    return guessedLetters               
</code></pre>

<p>Some issues in the code:</p>

<ul>
<li><code>randomWord</code> seems to be undefined.</li>
<li><code>guessedLetters</code> seems to be undefined as well.</li>
<li>There are 2 returns in the function</li>
</ul>

<p>If your code did run then maybe it never gets into the inner loop. Add some <code>print</code> statements at different places to see if the code at that point gets run.</p>

<p>Also note that you don't need to do <code>for j in range(len(letters))</code> just to access <code>letters[i]</code> later on. Just do:</p>

<pre><code>for letter in letters:
    # your code
</code></pre>

<p>It's shorter and has the same effect. The same for the inner loop.</p>

<p>Good luck!</p>

<p>Editted: try to print out <code>randomWord</code> before entering the function to see what the value of that variable is.</p>

python global-variables local