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

Time:08-04

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 use\n")

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: \n\n").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 remaining\n")
    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")

CodePudding user response:

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.

CodePudding user response:

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:
  • Related