Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

hangman in python

#simple version of hangman without the drawings 
#and made with an API for the random words

import requests

# variables
word = requests.get("https://random-word-api.herokuapp.com/word").json()[0]
string = ""
positionOfLetter = []
tries = 10

# create string from dashes
for letters in word:
    string = string + "_"
print("
The word is...")
print(string)


# the game
while "_" in string and tries > 0:
    guess = input("
Guess the a letter: ")
    if guess in word:
        for pos, char in enumerate(word):
            if(char == guess):
                positionOfLetter.append(pos)
        for position in positionOfLetter:
            string = string[:position] + guess + string[position+1:]
        print("That letter IS in the word!, you have " + str(tries) + " lifes left!")
    else:
        print("That letter is not in the word")
        tries -= 1
    positionOfLetter = []
    
    print(string)
    

# end of the game, the word is guessed
print("
you finished the game with " + str(tries) + " lifes left!, the word was:
" + "33[1m" + word + "33[1m")
 
PREVIOUS NEXT
Tagged: #hangman #python
ADD COMMENT
Topic
Name
2+1 =