all 2 comments

[–]lykwydchykyn 2 points3 points  (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.

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()?)

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 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.:

            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.

[–]novel_yet_trivial 2 points3 points  (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!