From cbd11ab4d1560301979c0ee1c60691ad33497561 Mon Sep 17 00:00:00 2001 From: Alisha Parveen <139856223+Alisha-786@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:53:11 +0530 Subject: [PATCH] Create word_guess_game.py --- Python/word_guess_game.py | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Python/word_guess_game.py diff --git a/Python/word_guess_game.py b/Python/word_guess_game.py new file mode 100644 index 0000000..1c2d871 --- /dev/null +++ b/Python/word_guess_game.py @@ -0,0 +1,53 @@ +import random + +# List of words to choose from +word_list = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew"] + +def choose_random_word(): + return random.choice(word_list) + +def play_game(): + print("Welcome to the Random Word Guessing Game!") + word_to_guess = choose_random_word() + guessed_letters = [] + attempts = 6 # You can change the number of allowed attempts + + while attempts > 0: + word_display = "" + for letter in word_to_guess: + if letter in guessed_letters: + word_display += letter + else: + word_display += "_" + + print("\nWord to guess: " + word_display) + print("Guessed letters: " + ", ".join(guessed_letters)) + print("Attempts remaining: " + str(attempts)) + + guess = input("\nGuess 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 already guessed that letter.") + continue + + guessed_letters.append(guess) + + if guess in word_to_guess: + print("Good guess!") + else: + print("Wrong guess!") + attempts -= 1 + + if "_" not in word_display: + print("\nCongratulations! You guessed the word: " + word_to_guess) + break + + if attempts == 0: + print("\nOut of attempts. The word was: " + word_to_guess) + +if __name__ == "__main__": + play_game()