Python hangman, when checking letter it always results as wrong

Viewed 48

I am learning Python recently and I am trying to make a hangman game. I followed the tutorial: https://www.youtube.com/watch?v=pFvSb7cb_Us and modified the code to my liking. However when trying to see if the player put in the correct letter it always results as wrong. I do notice that is because I did not make a way for it to check every letter but every attempt I made to do that ends in a error. Can anyone please help me?

also excuse me if I asked my question in the wrong way, I am new here on stackoverflow.

This is the part that checks for the letter:

            lengthWordToGuess = len(randomWord)
        amountWrong = 0
        currentGuess = 0
        currentLettersGuessed = []
        currentLettersRight = 0

        while(amountWrong != 6 and currentLettersRight != lengthWordToGuess):
            print("\nLetters guessed so far: ")
            for letter in currentLettersGuessed:
                print(letter, end=" ")
            letterGuessed = input("\nGuess a letter")
            #guessed right
            if(randomWord[currentGuess] == letterGuessed):
                hangmanVisualPrint(amountWrong)
                currentGuess += 1
                currentLettersGuessed.append(letterGuessed)
                currentLettersRight = giveWord(currentLettersGuessed)
            #guessed wrong
            else:
                amountWrong += 1
                currentLettersGuessed.append(letterGuessed)
                
                hangmanVisualPrint(amountWrong)
                
                currentLettersRight = giveWord(currentLettersGuessed)
2 Answers

no worries I am quite new too. so python has a cool feature where you can check if a variable is in a list using the in keyword link. So you can rewrite your code to check if(letterGussed in correctLetters) where correctLetters can either be the full word or a list of chars. You can check before this for if(letterGuessed in alreadyGuessed) and throw a response so they can guess the same letter twice. You can also do this with loops where you check every letter in a list or string.

Please show us the error code from the console. That is usually a good indicator as to which line we should be looking at.

Next why don't you upload all of your code? It may make more sense that way.

Then, use the print("") statement everywhere and visualize where your code is going wrong. I typically put a print() right before my IF statements.

Lastly, Make sure the data types from input() and the randomWord[currentGuess] are the same. For example, the int 5 may not always equal the char '5' or the string '5'.

One more thing, when it come's to new programmers, they tend to struggle with the index of strings. Perhaps you're checking the the wrong letter within 'example' as you could be comparing the 'x' but the input() was given 'e'.

Related