Home > Net >  problem with game that I am creating python
problem with game that I am creating python

Time:03-15

I was Creating this game , it is simply hangman is game which is the program choose a word randomly from a list , and the user guess a letter from the randomly chosen word , if the word is incorrect the lives count decrease until its depleted .

import random as rn
words = ['peter','apple','dentist']

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

chosen_word = rn.choice(words)
print(chosen_word)

blanks = []
lives = 6
state = False
for j in range(0 , len(chosen_word)):
  blanks  = '_'




while not state  :
  guess = input('Enter a letter : ')

  guess.lower()


  for position in range(len(chosen_word)) :
    letter = chosen_word[position]
    if letter == guess :
      blanks[position] = letter 
  print(blanks)   

  if '_' not in blanks :
    state = True
    print('Youy Won !')

  if guess not in letter :
    lives -= 1
    print(stages[lives])
    if lives == 0 :
      state = True
      print('You Lose !')

My problem is why it keeps printing the stages list even if the letter chosen is true ?

CodePudding user response:

Your problem is if guess not in letter:. You check in guess in wrong element.

It has to be if guess not in chosen_word:

That's all.


OR you should exit (break) loop when you found matching letter - but you run loop to the end - so you assign new values to letter and finally it has last letter from chosen_word which doesn't match to guess.

But this would replace only first letter matching to guess and skip other - ie. in apple if you choose p it would match only first p and skip second p - so first version is better.

    for position, letter in enumerate(chosen_word):
        if letter == guess :
            blanks[position] = letter
            break  # <--- exit loop when found letter

If you use print() to see what you have in variables then you should see mistake.


You have also wrong indentations.

Full working code with some small changes.

import random

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

words = ['peter','apple','dentist']

chosen_word = random.choice(words)
print(chosen_word)

blanks = ['_'] * len(chosen_word)
lives = 6
state = False

while not state  :
    guess = input('Enter a letter : ')

    guess = guess.lower()

    for position, letter in enumerate(chosen_word):
        if letter == guess :
            blanks[position] = letter
      
    print(blanks)   

    if '_' not in blanks:
        state = True
        print('Youy Won !')

    if guess not in chosen_word:
        lives -= 1
        
    print(stages[lives])
    
    if lives == 0:
        state = True
        print('You Lose !')
  • Related