Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A hangman game.
#1
A hangman type game I wrote. It's fully functional but I'm still trying to improve on it.
It does require a list of words in a .txt file. I used this list.

import random


def hang_man():
    """Hangman game"""

    with open("words_alpha.txt", "r") as f:
        lines = f.readlines()
        raw_word = random.choice(lines)

    letter_list = list(raw_word)
    del letter_list[-1] # Deletes \n charater.
    final_word = "".join(letter_list)

    vowels = ["a", "e", "i", "o", "u"]
    count = 0
    incorrect_letters = []
    spaces = ["-" for letter in final_word]

    for count in range(len(letter_list * 2)):
        print(f"There are {len(letter_list)} letters in the word.")
        guess_letter = input("Guess a letter. ")

        if guess_letter in letter_list:
            locate = letter_list.index(guess_letter)
            spaces[locate] = guess_letter
            
            if guess_letter in vowels:
                print(f"There is an {guess_letter} at {locate}.")
            else:
                print(f"There is a {guess_letter} at {locate}.")

            print("".join(spaces))

            count += 1
            while count >= len(final_word) \ 2:
                guess_word = input("Guess the word, y/n? ")
                if guess_word == "y":
                    guess = input("What is the word? ")
                    if guess in final_word:
                        print("You avoided death!")
                        raise SystemExit
                    else:
                        print("Wrong!")
                        break
                else:
                    break

        else:
            if guess_letter not in incorrect_letters:
                incorrect_letters.append(guess_letter)
            else:
                print("You already guessed that letter!")

            print(f"There is no {guess_letter}.")

    else:
        print(f"You're hung! The word was {final_word}.")


if __name__ == "__main__":
    hang_man()
Reply


Messages In This Thread
A hangman game. - by mcmxl22 - Oct-07-2019, 06:58 AM
RE: A hangman game. - by ichabod801 - Oct-07-2019, 02:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tk hangman game joe_momma 0 2,922 Aug-09-2019, 02:48 PM
Last Post: joe_momma
  Hangman game, feedback appreciated WolfWayfarer 10 8,153 Jul-19-2018, 08:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020