Easy way to code Hangman game with Python.

Hangman game.


Python is a great place to start game development, it's clear and clean syntactic structure is what makes it stand out above other programming languages for game design. Python is regarded as one of the most efficient languages for learning game development.

Hangman game with Python.



There are a lot of tools, modules, and frameworks in the Python environment to help you build games efficiently. So in other words you can do games starting from easy ones to the powerful videogames using Python existing external modules and frameworks.


Hangman game Python code:


For running this code correctly you will need files with corresponding ASCII Art and a wordlist, which is available in following REPL link: Wordlist and ASCII
Otherwise you can correct the code to ignore ASCII Art and intoduce wordlist in a list inside the code.




import random

from hangman_words import word_list

chosen_word = random.choice(word_list)
word_length = len(chosen_word)

end_of_game = False
lives = 6

from hangman_art import logo
print(logo)

display = []
for _ in range(word_length):
    display += "_"

while not end_of_game:
    guess = input("Guess a letter: ").lower()


    if guess in display:
        print(f"You've already guessed {guess}")

    
    for position in range(word_length):
        letter = chosen_word[position]
        
        if letter == guess:
            display[position] = letter

    if guess not in chosen_word:

        print(f"You guessed {guess}, that's not in the word. You lose a life.")
        
        lives -= 1
        if lives == 0:
            end_of_game = True
            print("You lose.")

    print(f"{' '.join(display)}")

    if "_" not in display:
        end_of_game = True
        print("You win.")

    
    from hangman_art import stages
    print(stages[lives])

OUT:
Guess a letter:





See also related topics: