Skip to content

Commit

Permalink
Merge pull request #82 from ananyag309/default
Browse files Browse the repository at this point in the history
added  Hangman Game with ASCII Art
  • Loading branch information
Ayu-hack authored Oct 5, 2024
2 parents 0ac3c48 + fd143b0 commit 9e24c5b
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions Hangman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import random

HANGMAN_PICS = [
"""
------
| |
|
|
|
|
--------
""",
"""
------
| |
| O
|
|
|
--------
""",
"""
------
| |
| O
| |
|
|
--------
""",
"""
------
| |
| O
| /|
|
|
--------
""",
"""
------
| |
| O
| /|\\
|
|
--------
""",
"""
------
| |
| O
| /|\\
| /
|
--------
""",
"""
------
| |
| O
| /|\\
| / \\
|
--------
""",
]

WORDS = ['python', 'javascript', 'hangman', 'development', 'terminal', 'project']

def get_random_word(word_list):
return random.choice(word_list).lower()

def display_board(missed_letters, correct_letters, secret_word):
print(HANGMAN_PICS[len(missed_letters)])
print("\nMissed letters:", ' '.join(missed_letters))

blanks = ['_' if letter not in correct_letters else letter for letter in secret_word]
print(" ".join(blanks))

def is_valid_guess(guess, missed_letters, correct_letters):
# Check if the input is a single alphabetic character
if len(guess) != 1 or not guess.isalpha():
print("Invalid input! Please enter a single letter.")
return False
# Check if the letter has already been guessed
if guess in missed_letters or guess in correct_letters:
print("You've already guessed that letter! Try again.")
return False
return True

def play_hangman():
secret_word = get_random_word(WORDS)
missed_letters = []
correct_letters = []

while len(missed_letters) < len(HANGMAN_PICS) - 1:
display_board(missed_letters, correct_letters, secret_word)

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

if is_valid_guess(guess, missed_letters, correct_letters):
if guess in secret_word:
correct_letters.append(guess)
if all(letter in correct_letters for letter in secret_word):
print(f"Congratulations! You've guessed the word '{secret_word}'.")
break
else:
missed_letters.append(guess)

if len(missed_letters) == len(HANGMAN_PICS) - 1:
display_board(missed_letters, correct_letters, secret_word)
print(f"Sorry, you've been hanged! The word was '{secret_word}'.")
break

if __name__ == "__main__":
while True:
play_hangman()
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again != 'yes':
print("Thanks for playing!")
break







0 comments on commit 9e24c5b

Please sign in to comment.