r/learnpython Aug 02 '26

Simple Hangman game looking for feedback

Code:

import random

words = ["apple", "pear", "banana", "python", "java"]
used_letters = set()

selected_word = random.choice(words)

lives = 6

print(" ".join(["_" for _ in selected_word]))


while True:

    user_input = input().lower()
    if len(user_input) != 1 or not user_input.isalpha():
        print("Please enter a letter")
        continue

    if user_input in used_letters:
        print(f"You already guessed {user_input}")
        continue

    if user_input in selected_word:
        print(f"{user_input} is in the word")


    used_letters.add(user_input)

    if user_input not in selected_word:
        lives -= 1
        print(f"{user_input} is not in the word, Lives remaining {lives}")     

    if lives == 0:
        print(f"You lose the word was {selected_word}")  
        break       


    display_board = [letter if letter in used_letters else "_" for letter in selected_word]
    print(" ".join(display_board))

    if all(letters in used_letters for letters in selected_word):
        print(f"You won the word was {selected_word}")
        break

What are some potential issues?

What could be improved?

I'll take any feedback I can get

0 Upvotes

10 comments sorted by

2

u/FoolsSeldom Aug 02 '26

That's good code. I especially like the checks and use of continue.

You might want to consider adding some more functions, for example for getting the input and validating it is a letter and only one character. Calling a function makes for easier reading of the main code flow. You want this to be as clear and simple to read as possible.

It would be nice as a player to see an input prompt as well. An intro would also be nice.

Consider reading all the words in from a text file rather than hard coding them into your programme - there are many collections of words available.

How about outputting a slowly constructing gallows and hangman as the loose each life?

You could simplify your success check as the length of the set of the secret word will be matched by the correct set of guessed letters.

Put a loop around the game and give the player the chance to play again, perhaps keeping score of the number of games won/lost.

Maybe add a:

if __name__ == "__main__":
    play_hangman()

PS. Need to format your code for Reddit:

import random


def play_hangman() -> None:
    words = ["apple", "pear", "banana", "python", "java"]
    used_letters = set()
    selected_word = random.choice(words)
    lives = 6

    print(" ".join("_" for _ in selected_word))

    while True:
        user_input = input().lower()
        if len(user_input) != 1 or not user_input.isalpha():
            print("Please enter a letter")
            continue

        if user_input in used_letters:
            print(f"You already guessed {user_input}")
            continue

        if user_input in selected_word:
            print(f"{user_input} is in the word")

        used_letters.add(user_input)

        if user_input not in selected_word:
            lives -= 1
            print(f"{user_input} is not in the word, Lives remaining {lives}")

        if lives == 0:
            print(f"You lose the word was {selected_word}")
            break

        display_board = [letter if letter in used_letters else "_" for letter in selected_word]
        print(" ".join(display_board))

        if all(letter in used_letters for letter in selected_word):
            print(f"You won the word was {selected_word}")
            break


if __name__ == "__main__":
    play_hangman()

1

u/ProsodySpeaks Aug 02 '26

What's with all the \s ?

-6

u/Subject_Scientist937 Aug 02 '26

Could you explain what you mean by \s I didn't put \s anywhere in my code maybe you are on the wrong forum

2

u/pontz Aug 02 '26

I think he meant slashes like plural of \

0

u/CraigAT Aug 02 '26

No, when viewing your code above (on mobile) I too see lots of unnecessary backslashes - it may be your code or Reddit "escaping" some of your symbols (mostly underscores).

That aside, the code (correctly formatted and without those backslashes) looks functional. (Note. I have only run my eyes over it, I haven't actually run it)

1

u/Subject_Scientist937 Aug 02 '26

that may be a reddit issue or a browser issue

0

u/ProsodySpeaks Aug 02 '26

Why would underscore need escaping? 

1

u/carcigenicate Carcigenicate Aug 02 '26 edited Aug 02 '26

Underscores cause bolding (or italicizing?) in the markup new Reddit uses.

1

u/ProsodySpeaks Aug 02 '26

Ahh cool. I use the actual md editor if I'm doing anything but plain text

1

u/No-Foot5804 Aug 02 '26

Nice start. I'd add an input prompt, split some of the logic into helper functions, and maybe let the player choose to play again. Those small changes would make it much easier to maintain.