-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import random | ||
|
||
def choose_random_word(): | ||
word_list = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew"] | ||
return random.choice(word_list) | ||
|
||
def display_word(word, guessed_letters): | ||
display = "" | ||
for letter in word: | ||
if letter in guessed_letters: | ||
display += letter | ||
else: | ||
display += "_" | ||
return display | ||
|
||
def hangman(): | ||
secret_word = choose_random_word() | ||
guessed_letters = [] | ||
attempts = 6 # Number of attempts allowed | ||
|
||
print("Welcome to Hangman!") | ||
|
||
while attempts > 0: | ||
print(display_word(secret_word, guessed_letters)) | ||
guess = input("Guess a letter: ").lower() | ||
|
||
if len(guess) != 1 or not guess.isalpha(): | ||
print("Please enter a single letter.") | ||
continue | ||
|
||
if guess in guessed_letters: | ||
print("You've already guessed that letter.") | ||
continue | ||
|
||
guessed_letters.append(guess) | ||
|
||
if guess in secret_word: | ||
print("Good guess!") | ||
if display_word(secret_word, guessed_letters) == secret_word: | ||
print("Congratulations, you've won! The word was:", secret_word) | ||
break | ||
else: | ||
print("Incorrect guess.") | ||
attempts -= 1 | ||
print(f"You have {attempts} attempts left.") | ||
|
||
if attempts == 0: | ||
print("Sorry, you've run out of attempts. The word was:", secret_word) | ||
|
||
if __name__ == "__main__": | ||
hangman() |