Why does my code output different results when using the if statement in a while loop?

Question:

In the while loop

When I use "if letter != guess" the "You Lose" works well but not the "You win"- it displays a reduction in lives_left and the ASCII art as loosing lives when it shouldn’t

When I use "if letter not in chosen word" the "You win" works well but not the "You Lose"- it keeps looping to "guess a letter"

Please explain both cases and solution

import random

stages = ['''
  +---+
  |   |
  O   |
 /|  |
 /   |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']


word_list = ["cat"]
chosen_word = random.choice(word_list)

#Testing code
print(f'Pssst, the solution is {chosen_word}.n')

chosen_word_list = []


lives_left = 6
print(f"You have {lives_left} lives to usen")

for i in range(len(chosen_word)):
 chosen_word_list.append("_")

print(f"{chosen_word_list}n")


game_end = False


while not game_end:
  
  
    
  guess = input("Guess a letter: nn").lower()
  

  for i in range(len(chosen_word)):
    letter = chosen_word[i]

    
    if letter == guess:
      chosen_word_list[i] = letter


      print(f"n{chosen_word_list}")
      print(stages[6])


  
  if letter != guess:
    lives_left -= 1
    print(f"You have {lives_left} lives remainingn")
    print(stages[lives_left])
    
    if lives_left == 0:
      game_end = True
      print("You lose")

  
  
    
  chosen_word_list_str = " ".join(chosen_word_list)
  chosen_word_replace_space = chosen_word_list_str.replace(" ","")
  # print(chosen_word_replace_space)

  
  if chosen_word == chosen_word_replace_space:
    game_end = True
    print("You win")
Asked By: Handsome rob

||

Answers:

When I use "if letter != guess" the "You Lose" works well but not the "You win"- it displays a reduction in lives_left and the ASCII art as loosing lives when it shouldn’t

That’s because at that moment letter is the last letter that was visited in the loop that had just been executed before this if. That makes no sense, and in most cases this expression will be true.

When I use "if letter not in chosen word" the "You win" works well but not the "You Lose"- it keeps looping to "guess a letter"

That’s because letter is the last letter in chosen_word (see above reasoning) and so this condition cannot be true.

You need to test guess, not letter. So:

    if guess not in chosen_word:
Answered By: trincot

First problem: "if letter != guess"

You keep cycling through the letters of chosen_word even after you found a match.
For the example "cat" if you choose ‘a’ then letter == guess is True for i = 1.

However the for loop keeps going and when you get to letter != guess the value of your letter is ‘t’ instead of ‘a’.

The fix is easy. Just add a break to the end of if letter == guess

for i in range(len(chosen_word)):
    letter = chosen_word[i]

    
    if letter == guess:
      chosen_word_list[i] = letter


      print(f"n{chosen_word_list}")
      print(stages[6])
      break

Sorry but I did not understand what you were asking on the second problem.

Answered By: Yellow

Instead of placing your condition inside the ‘for’ loop, check if the letter is in the chosen_word array (a string is also an array)

if guess in chosen_word:
  chosen_word_list[chosen_word.index(guess)] = guess
  print(f"n{chosen_word_list}")
  print(stages[6])

else:
  lives_left -= 1
  print(f"You have {lives_left} lives remainingn")
  print(stages[lives_left])

and it works.

Answered By: michel duturfu
import random

stages = ['''
  +---+
  |   |
  O   |
 /|  |
 /   |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''', '''
  +---+
      |
      |
      |
      |
      |
=========
''']


#you can't have two same letters in one word
word_list = ["cat", "dog", "duck", "chair", "house", "sky", "nose", "brick", "car", "clothes", "sea"]
chosen_word = random.choice(word_list)

#Testing code
#print(f'Pssst, the solution is {chosen_word}.n')

chosen_word_list = []


lives_left = len(stages)-1
print(f"You have {lives_left} lives to usen")
print(stages[lives_left])

for i in range(len(chosen_word)):
 chosen_word_list.append("_")

print(f"n{chosen_word_list}n")


game_end = False


while not game_end:
  
  
    
  guess = input("Guess a letter: nn").lower()
  

  if guess in chosen_word:
    chosen_word_list[chosen_word.index(guess)] = guess
    print(f"n{stages[lives_left]}")
    print(f"n{chosen_word_list}n")

  
  else:
    lives_left -= 1
    print(f"You have {lives_left} lives remainingn")
    print(stages[lives_left])
    print(f"n{chosen_word_list}n")
    
    if lives_left == 0:
      game_end = True
      print(f"You losenthe word was {chosen_word}")

  
  
    
  chosen_word_list_str = " ".join(chosen_word_list)
  chosen_word_replace_space = chosen_word_list_str.replace(" ","")
  # print(chosen_word_replace_space)

  
  if chosen_word == chosen_word_replace_space:
    game_end = True
    print("You win")
Answered By: michel duturfu