()
| 20 | return random.choice(words).strip().lower() |
| 21 | |
| 22 | def main(): |
| 23 | secret_word = get_random_word_from_file("words.txt") |
| 24 | guessed = set() |
| 25 | max_attempts = 6 |
| 26 | attempts = 0 |
| 27 | |
| 28 | print("Welcome to Hangman!") |
| 29 | print("Try to guess the word!") |
| 30 | |
| 31 | while attempts < max_attempts: |
| 32 | print("Word:", end=" ") |
| 33 | display_word(secret_word, guessed) |
| 34 | |
| 35 | guess = input("Guess a letter: ").lower() |
| 36 | |
| 37 | if not is_valid_guess(guess): |
| 38 | print("Invalid guess. Please enter a lowercase letter.") |
| 39 | continue |
| 40 | |
| 41 | if guess in guessed: |
| 42 | print("You've already guessed that letter.") |
| 43 | continue |
| 44 | |
| 45 | guessed.add(guess) |
| 46 | |
| 47 | if guess not in secret_word: |
| 48 | attempts += 1 |
| 49 | print("Incorrect guess. Attempts remaining:", max_attempts - attempts) |
| 50 | elif all(letter in guessed for letter in secret_word): |
| 51 | print("Congratulations! You've guessed the word:", secret_word) |
| 52 | break |
| 53 | |
| 54 | if attempts == max_attempts: |
| 55 | print("Sorry, you've run out of attempts. The word was:", secret_word) |
| 56 | |
| 57 | play_again = input("Do you want to play again? (Y/N): ").lower() |
| 58 | if play_again == 'y': |
| 59 | main() |
| 60 | else: |
| 61 | print("Thanks for playing!") |
| 62 | |
| 63 | if __name__ == "__main__": |
| 64 | main() |
no test coverage detected