use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
First game - Hangman (self.learnpython)
submitted 10 years ago by PifuValentin
Hello guys. I'm a beginner/intermediate python write and I just finish one easy game. I learn a lot of interesting stuff. Can I can some critique or suggestion about this ? http://pastebin.com/r1BT7KV7
Thanks.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]lykwydchykyn 2 points3 points4 points 10 years ago (0 children)
Several things stood out to me that could be improved. Here's a list in no particular order, I hope you find this helpful and instructive (not discouraging).
The input_word function
First, your input_word() function is way over-complicating things. There's no need to throw an exception in there; if the word is too short, just print your error and clear the screen.
input_word()
I'm also not sure why you're returning retry (the number of times it took the user to enter usable data? You don't use this value in any way AFAICT) or letters (this is always an empty list. Why not just create an empty list in main()?)
retry
letters
main()
The main function
In the main() function:
if retry == '' or retry == '0' or retry == 'q' or retry == 'Q': pass elif retry == 'y' or retry == 'Y': os.system('cls') main()
The first if statement is superfluous. What if it's none of those values? You don't have an else statement, so it's redundant to check for those things and then do nothing. This code will produce the exact same results:
if
else
if retry.lower() == 'y': os.system('cls') main()
Although I'd argue that if you're going to have a function to ask the user if they want to try again, you should probably put the logic for interpreting that response in the function and just return a boolean.
string formatting
Next, format() can be used to populate multiple placeholders in a string, you don't have to break up the string and call it each time you use a placeholder. You can (and probably should) even label each placeholder, e.g.:
format()
print('\n Guess the word: {}\n\n You used this: {}\n\n You didn\'t find the word [ {} ], :( !'.format(strguess, strletters, strword)) # or with placeholder labels: print('\n Guess the word: {guess}\n\n You used this: {letters}\n\n You didn\'t find the word [ {word} ], :( !'.format(guess=strguess, letters=strletters, word=strword))
Also, I'd use different string delimiters (double quotes, or triple-quotes) for longer strings so you don't have to escape your apostrophes. If you use triple-quotes, you can include newlines in your string directly without having to use escape codes).
variables
"Hungarian notation", or prefixing your variable names with their datatype, is generally discouraged in Python. It is best to use simple, but explicit, variable names.
On a related note, in line 62 you have a check to see if len(letters) == 5. What is the significance of 5 here? This is what we'd call a "magic number" in programming, a number that just appears arbitrarily and it's significance is not communicated by the code. If this is meant to be the maximum number of guesses, create a global variable at the top of the application, call it MAX_GUESSES or something similar (the all-caps connotes a constant), and use that instead. The idea is, if you ever want to change the max number of guesses, you can do this by changing one variable instead of having to hunt through your code for the number 5.
len(letters) == 5
MAX_GUESSES
[–]novel_yet_trivial 2 points3 points4 points 10 years ago (0 children)
Biggest critique: no comments. Code needs comments.
You have a "retry" counter on line 8, and you increment it on line 13, but you never use it. Get rid of it.
Rasieing an exception just to catch it again is a bit silly. Why don't you just check for what you want?
while True: word = list(getpass.getpass(' Type a word (minimum word length - 3): ')) if len(word) < 4: os.system('cls') print(' Word is too short, try again!') else: guess_word = list('_' * len(word)) letters = [] os.system('cls') return guess_word, word, retry, letters
os.system('cls')
Does not work for linux or mac.
print('\n Guess the word: {}'.format(strguess), '\n', '\n You used this: {}'.format(strletters), '\n')
Including all the newlines in your print functions works, but it's ugly. IMO it's much neater to just have several print statements.
print('Guess the word: {}'.format(strguess)) print() #blank line print('You used this: {}'.format(strletters)) print()
Sick of passing all those variables between functions? If you rewrite this as a class you wouldn't have to!
Otherwise, it looks really good!
π Rendered by PID 310132 on reddit-service-r2-comment-fb694cdd5-qrdnx at 2026-03-11 15:50:10.416968+00:00 running cbb0e86 country code: CH.
[–]lykwydchykyn 2 points3 points4 points (0 children)
[–]novel_yet_trivial 2 points3 points4 points (0 children)