Python Hangman Game. Python 3
I am trying to create a simple Hangman game using Python. I have faced a problem which I can't solve and would be grateful to receive some tips from you guys. Okay I will provide y
Solution 1:
You could increment count
whenever you replace an underscore with the correct the letter. That way, count
will be equal to the number of letters correct in the word so far.
To be more clear, move count += 1
to be in the if
statement when you replace underscores with the actual letter.
One problem I see with this is you're giving the player the first letter and initializing count
to 1. I don't know why you're doing this, but if the first letter occurs more than once, it won't be reflected in the word, and the player will still have to guess that letter anyways.
Solution 2:
Move the count to where you are replacing the letter.
while user_guess:
if user_guess in word:
print("Correct letter.")
# replacing the '_' with the letter
for letter in range(len(word)):
if user_guess == word[letter]:
# adding 1 to count for every guessed letter
count += 1
secret_word[letter] = word[letter]
# here I am checking if the user has won
if count == len(word):
print("You Win!")
Post a Comment for "Python Hangman Game. Python 3"