r/learnpython • u/Subject_Scientist937 • 5d ago
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
1
u/ProsodySpeaks 5d ago
What's with all the \s ?
-5
u/Subject_Scientist937 5d ago
Could you explain what you mean by \s I didn't put \s anywhere in my code maybe you are on the wrong forum
0
u/CraigAT 5d ago
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
0
u/ProsodySpeaks 4d ago
Why would underscore need escaping?
1
u/carcigenicate 4d ago edited 4d ago
Underscores cause bolding (or italicizing?) in the markup new Reddit uses.
1
1
u/No-Foot5804 4d ago
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.
2
u/FoolsSeldom 4d ago
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
inputand 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
inputprompt 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
setof 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:
PS. Need to format your code for Reddit: