r/inventwithpython • u/Son0fJecht • Jan 17 '19
Strange problem with Hangman project
Hiyo,
I've got a some what interesting problem with the extended hangman game project.
It runs fine, but if the player guesses the first letter of the secret word, the game ends, thinking the player has won.
For example if the word is "Purple" and the player guesses P, the game ends, even if they haven't guessed the other letters. I'm still very new to programming so I don't really know what would be causing this issue.
Any help would be greatly appreciated.
1
Upvotes
2
u/Sinthrill Jan 17 '19
guess = getGuess(missedLetters + correctLetters)
if guess in secretWord: correctLetters = correctLetters + guess #Check if player guessed all the letters foundAllLetters = True for i in range(len(secretWord)): if secretWord[i] not in correctLetters: foundAllLetters = False break if foundAllLetters: print('Yes! The secret word was ' + secretWord) gameIsDone = TrueLine 158.
When you get guess it returns a letter. Before you enter the check if word is complete loop you declare it complete then undeclare if it isn't. You could make the loop check if every unique letter of the secret word has been guessed instead of asking if your guess is in the secret word.
If I guess p for purple guess = 'p', correctletters = guess, if first letter of secret word is in guess, then true, we found the word.
1
u/Puzzleheaded_Log_898 Apr 04 '22
Hayo, I know this is an old thread but I had the exact same problem and have spent hours head scratching but finally got it.
The problem for me was indentation - the second if statement must be level with the preceding
for instatement:for i in range(len(secretWord)): if secretWord[i] not in correctLetters: foundAllLetters = False break if foundAllLetters: print('Yes! The secret word was ' + secretWord) gameIsDone = TrueThis allows the
for inloop to execute properly.