1

I just would need a way to be able to end my hangman man game through check if every letter the has entered is in the list.

What I was thinking was whenever I append the letter they type and if it is in the actual letter list I would remove it from the actual letter list until there are no more elements in the list and compare it in an if statement to when it zero, and the user would win the game.

Would this be the most effective way?

import random 

Words = ["set", "scar", "cat"] 

Letters_Guessed= ""

Guess = random.choice(Words) 

ActualLetter = list(Guess) #use this to divide up the random word
print(ActualLetter)

Numb_guess = 0
max = 6 

HANGER = [
'''
  +---+
  |   |
      |
      |
      |
      |
=========
''',
'''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''',
'''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''',
'''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========
''',
'''
  +---+
  |   |
  O   |
 /|\  |
      |
      |
=========
''',
'''
  +---+
  |   |
  O   |
 /|\  |
   \  |
      |
=========
''',
'''
  +---+
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''']













letterAmount = ""
for letter in Guess:
        letterAmount += "_"
        print(letterAmount)


game = True
def guess(UserguessL): 
    global Numb_guess, game, letterAmount, ActualLetter, Letters_Guessed, max
    while game == True: 
        print(letterAmount)
        if UserguessL in ActualLetter: 
            UserguessL = input("Good job! What another word? ")
            Letters_Guessed += UserguessL
            letterAmount = ""
            print("Letters you have guessed: ", Letters_Guessed,)
            

        elif UserguessL not in ActualLetter:
            UserguessL = input("try again! ")
            Letters_Guessed += UserguessL
            Numb_guess += 1
            print("Letters you have guessed: ", Letters_Guessed, )
        print(HANGER[Numb_guess])
        print(letterAmount)
    if Numb_guess == max: 
            print("the word was", Guess)
            game = False
        


UserguessL= input("Welcome to Hangman, player! In this game you must guess a word with letters, correctly within a limited amount of guesses. \n What is the first letter you like to guess? ")
guess(UserguessL)
  • What defines _effective_ for you? Why do you think this could be not _effective enough_? If it works for you, it's probably OK... but you haven't explained how do you plan to remove the letters (`list.remove` won't do it since it removes only the first occurrence). – Ignatius Reilly Apr 18 '23 at 01:37
  • BTW, your criteria for naming changes randomly through your code (e.g. `letterAmount`, `ActualLetter`, `Letters_Guessed`...), which makes difficult for others to understand what you're trying to do. [PEP 8 – Style Guide for Python Code](https://peps.python.org/pep-0008/) – Ignatius Reilly Apr 18 '23 at 01:42
  • But would list.remove() not repeatedly run as the method is running? And so could I not just do that until there is nothing left. Also thanks you I will keep the naming convention and wording precise. – dazzlinghermit Apr 18 '23 at 02:03
  • Yes, but what if the word to guess is `elderberries` and the user proposes an `e`? You should use `list.remove` four times. Check this [answer](https://stackoverflow.com/a/21510181/15032126) :) – Ignatius Reilly Apr 18 '23 at 02:33

2 Answers2

0

As a fellow on this journey, I wish not to change all the codes. So, if I can minimize the change, I wish to add "game" to the following line.

global Numb_guess, max, letterAmount, ActualLetter, Letters_Guessed

And I just ran your code with the above fix, I found HANGER image does not show when I guessed the wrong letter.

Another note is that I would create another file just for HANGER image and Words as it can be good practice for import module.

  • 1
    No. `game` and `Numb_guess` should not be globals and should be defined inside the body of `guess` since it only makes sense to initialize them with one specific value. `max` shouldn't be called like that because it shadows the homonimous built-in and it should be declared as a keyword parameter e.g. `def guess(UserguessL, max_attempts=6):`. And we could go on with the rest... _Maybe_ `HANGER` would do well as a global. – Ignatius Reilly Apr 26 '23 at 18:13
0

This is a method that probably takes more steps, but saves your original string as you may need it for something else:

Have two strings, "answer" and "guess". If a letter guessed by the user is in the "answer" string, add it to the "guess" string. Then, continue until the user guesses another letter. Do as last time, and repeat this process until all the letters are guessed. The string will only add letters if they are correct, so no reason to place it in the same location. When the two string lengths match, the player wins.

Hope this helps!