1
\$\begingroup\$

Anything I can improve?

def main():
    word = 'nedim'
    hidden_word = list('_'*len(word))
    wrong_guess, amount_of_letters, guesses, done = 0, 0, 4, False

    while(wrong_guess != 5 and not done):
        guess = raw_input("Guess a letter: ")
        if len(guess) == 1 and guess.isalpha():
            while(guess in hidden_word):
                if guess in hidden_word:
                    print "Letter already exists!"
                    guess = raw_input('Try again: ')
            for letter in word:
                if letter == guess:
                    hidden_word[word.index(guess)] = guess
                    print ''.join(hidden_word)
                    if ''.join(hidden_word) == word:
                        print "\n ######## \n You won! \n ######## \n"
                        done = True
            if guess not in word:
                wrong_guess += 1
                if(guesses-wrong_guess < 0):
                    print "You have no chances left. Loser!"
                else:
                    print "You have ",guesses-wrong_guess," chances left."
        else:
            print "INVALID GUESS! \nRULES: \n\t > One guess at a time \n\t > Only english alphabet \n\t > Only Letters"

if __name__ == '__main__':
    main()
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to CodeReview. It will be nice to have a summary of what your code does . This is to aid reviewers who have no knowledge of Hangman game. \$\endgroup\$ – Siobhan Nov 1 '16 at 12:47
2
\$\begingroup\$

I see a number of things that may help you improve your code.

Eliminate unused variables

Unused variables are a sign of poor code quality, so eliminating them should be a priority. In this code, amount_of_letters is set to 0 but never actually used.

Use better function names

The name main might be find if this were a C program, but there is no requirement or advantage to using the name main in a Python program. Instead, I'd name it something like hangman.

Fix bug #1

At some point, if one is unsuccessful in guessing the word, the program will display the message:

You have  0  chances left.
Guess a letter: 

If I have 0 chances left, why am I being asked to guess again?

Fix bug #2

If the word contains multiple copies of the same letter, such as "maxim", if the user guesses a, x and m in that order, they'll see this:

Guess a letter: m
max__
max__

Use a list comprehension

You might not have studied Python long enough to learn about them, but I'd use a list comprehension here. Instead of the for letter in word loop, I'd write this:

hidden_word = [guess if word[i]==guess else ch for i, ch in enumerate(hidden_word)]

This looks complex, perhaps, but it's actually pretty easy to understand once you've mastered list comprehensions.

Set the case for all letters

At the moment, if the user guesses 'M' but the word is 'nadim', the program will count that as a bad guess. In the traditional hangman game, no distinction is made between capital and lowercase letters. You might consider handling that for your program, such as by forcing all letters to be lowercase for example.

Use variables consistently

The guesses variable looks like it's supposed to be the maximum number of guesses allowed, but then we also see a hard-coded 5 in the program at the top of the loop. Choose one (I'd prefer a named constant such as max_guesses) and stick with it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.