Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python hangman, when checking letter it always results as wrong

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

            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)

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading