List index while loop python
<p>I have an issue with the following code. When i typed the exact secret words character, the following code wont match the secret codes. </p>

<pre><code>#initiate words for guessing secretWords =['cat','mouse','donkey','ant','lion']

#Generate a random word to be guessed generateWord = (random.choice(secretWords))

# User have attempts only to the random generate words LeftCount = 6

generateWord = ["_"] * len(secretWords) userInput="" LetterNumber=0 RightGuess =0 

while (LeftCount !=0):
     print ("Word Contains", len(generateWord),"letters")
     print ("You have", str(LeftCount),"attempts remaining")
     print ("\n")
     print(generateWord)
     print ("\n")
     userInput=input("Enter word: ")
     while(LetterNumber&lt; len(generateWord)):
         if(generateWord[LetterNumber] == userInput):
             generateWord[LetterNumber]= userInput

             RightGuess +=1
         LetterNumber +=1
     LeftCount -=1
     LetterNumber=0

     if (RightGuess == len(generateWord)):
         print ("Congratulations")
         break

     if(LeftCount ==0):
         print ("Game over")
</code></pre>

<p>Why are you comparing one letter in generateWord to the entire userInput?</p>

<pre><code>if(generateWord[LetterNumber] == userInput):
</code></pre>

<p>That line compares the character at "LetterNumber" index to the userInput, so if a user enters a word it will never return true. </p>

<p>If you're attempting to count the number of correct letters in the users guess, shouldn't you be comparing each letter from the user input with the corresponding letter in the "generateWord". </p>

<pre><code>if(generateWord[LetterNumber] == userInput[LetterNumber]):
</code></pre>

<p>Also some general points, variable names shouldn't start with a capital, it should be "letter_number" according to Python standards. Try to improve your variable names as well, maybe call it "generated_word", not "generate_word". "Generate_word" implies it is a function, as generate is a verb.</p>

<p>The line after the if statement also reassigns the entire userInput into the generateWord value, at the letter index, why are you doing that?</p>

<p>Finally, you need to generate a new word at the end of your while loop, or maybe the beginning, as at the moment you only generate a word at the beginning, then it will use the same word in each iteration.</p>

<p><strong>Try to use print to print out some of your variables, it will help you debug your program, as it's definitely not doing what you expect.</strong></p>

python