I am trying to make a word guessing game. Everything works except for the last if statement part. If the user guesses the name correctly, it doesn't print "Won!". However, if not, else statement executes and it does print "Lost!". Why is that? I checked it many times, but I coulnd't find any error.
Note: I use Python 3.6
import random
def get_random_word():
words = ["Ronnie", "Deskty", "Lorrie"]
word = words[random.randint(0, len(words)-1)]
return word
def show_word(word):
for character in word:
print(character, " ", end="")
print("")
def get_guess():
print("Enter a letter: ")
return input()
def process_letter(letter, secret_word, blanked_word):
result = False
for i in range(0, len(secret_word)):
if secret_word[i] == letter:
result = True
blanked_word[i] = letter
return result
def print_strikes(number_of_strikes):
for i in range(0, number_of_strikes):
print("X ", end="")
print("")
def play_word_game():
strikes = 0
max_strikes = 3
playing = True
word = get_random_word()
blanked_word = list("_" * len(word))
while playing:
show_word(blanked_word)
letter = get_guess()
found = process_letter(letter, word, blanked_word)
if not found:
strikes += 1
print_strikes(strikes)
if strikes >= max_strikes:
playing = False
if not "_" in blanked_word:
playıng = False
if strikes >= max_strikes:
print("Lost")
else:
print("Won")
print("Game started")
play_word_game()
print("Game over")
Look at this piece of code:
if not "_" in blanked_word:
playıng = False
You have an ı
(U+0131 LATIN SMALL LETTER DOTLESS I) in place of i
. Python will happily create the variable playıng
for you (unlike other languages where variables have to be declared). Therefore playing
never gets updated to False
in the winning case and the function never exits.
As Martijn points out in the comments, a linter may help by spotting misspelled variables and other likely errors. Consider using one in your editor or IDE. Two popular linters are Pylint and Flake8.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With