|
| 1 | +# import random |
| 2 | +# |
| 3 | +# print("Welcome to Hangman!") |
| 4 | +# |
| 5 | +# words = ["hacker", "bounty", "random"] |
| 6 | +# secret_word = random.choice(words) |
| 7 | +# print("You get 5 guesses") |
| 8 | +# display_word = [] |
| 9 | +# for letter in secret_word: |
| 10 | +# display_word += "_" |
| 11 | +# print(display_word) |
| 12 | +# |
| 13 | +# num = 0 |
| 14 | +# game_over = False |
| 15 | +# |
| 16 | +# while not game_over: |
| 17 | +# guess = input("Guess a letter ").lower() |
| 18 | +# for position in range(len(secret_word)): |
| 19 | +# letter = secret_word[position] |
| 20 | +# if letter == guess: |
| 21 | +# display_word[position] = letter |
| 22 | +# if guess not in secret_word: |
| 23 | +# num += 1 |
| 24 | +# guesses_left = 5 - num |
| 25 | +# print(f"You have {guesses_left} guesses left") |
| 26 | +# if num >= 5: |
| 27 | +# print("You Loser") |
| 28 | +# game_over = True |
| 29 | +# print(display_word) |
| 30 | +# |
| 31 | +# if "_" not in display_word: |
| 32 | +# print("You Win") |
| 33 | +# game_over = True |
| 34 | + |
| 35 | + |
| 36 | +import random |
| 37 | + |
| 38 | +# List of words to choose from |
| 39 | +word_list = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew"] |
| 40 | + |
| 41 | +# Select a random word from the list |
| 42 | +secret_word = random.choice(word_list) |
| 43 | + |
| 44 | +# Initialize variables |
| 45 | +remaining_guesses = 5 |
| 46 | +guessed_letters = [] |
| 47 | +display_word = ["_"] * len(secret_word) |
| 48 | + |
| 49 | +# Main game loop |
| 50 | +while remaining_guesses > 0: |
| 51 | + # Display the current state of the word |
| 52 | + print(" ".join(display_word)) |
| 53 | + print(f"Remaining guesses: {remaining_guesses}") |
| 54 | + |
| 55 | + # Ask the player for a guess |
| 56 | + guess = input("Guess a letter: ").lower() |
| 57 | + |
| 58 | + # Check if the guess is a single letter |
| 59 | + if len(guess) != 1 or not guess.isalpha(): |
| 60 | + print("Please enter a single letter.") |
| 61 | + continue |
| 62 | + |
| 63 | + # Check if the letter has already been guessed |
| 64 | + if guess in guessed_letters: |
| 65 | + print("You've already guessed that letter.") |
| 66 | + continue |
| 67 | + |
| 68 | + # Add the guessed letter to the list of guessed letters |
| 69 | + guessed_letters.append(guess) |
| 70 | + |
| 71 | + # Check if the guessed letter is in the secret word |
| 72 | + if guess in secret_word: |
| 73 | + for i in range(len(secret_word)): |
| 74 | + if secret_word[i] == guess: |
| 75 | + display_word[i] = guess |
| 76 | + else: |
| 77 | + print("Incorrect guess.") |
| 78 | + remaining_guesses -= 1 |
| 79 | + |
| 80 | + # Check if the player has guessed the entire word |
| 81 | + if "".join(display_word) == secret_word: |
| 82 | + print("Congratulations! You've guessed the word:", secret_word) |
| 83 | + break |
| 84 | + |
| 85 | +# Game over |
| 86 | +if "".join(display_word) != secret_word: |
| 87 | + print("Sorry, you're out of guesses. The word was:", secret_word) |
| 88 | + |
0 commit comments