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)
>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.