Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

hangman python

while(tries > 0):
        #variable to count number of incorrect attempts
            incorrect = 0

            for letter in word:     
                        if letter in totalGuesses:
                        #print the correctly guessed letter if found
                                    print(letter)
                        else:
                        #print a dash if letter not in word
                                    print("_")
                                    incorrect += 1

            if incorrect == 0:
                        #player wins
                        print("You Win")

                        print("Word is - ", word)
                        break

            #input again if wrong letter guessed
            guess = input("
Guess a letter in the word - ")

            #store guessed letters in totalGuesses
            totalGuesses += guess

            #if guess in not present in the word
            if guess not in word:               
                        tries -= 1
                        print("Wrong")

                        #print tries left for the player
                        print("You have ", + tries, 'guesses left')


                        if tries == 0:
                                    print("You Lose")
Source by dev.to #
 
PREVIOUS NEXT
Tagged: #hangman #python
ADD COMMENT
Topic
Name
4+8 =