boyan1324

kati

May 28th, 2025
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. import random
  2.  
  3. # Думи за игра
  4. words = ["компютър", "училище", "игра", "часовник", "кола", "пъзел"]
  5.  
  6. # Избиране на дума
  7. chosen_word = random.choice(words)
  8. hidden_letters = ["_" for _ in chosen_word]
  9. guessed_letters = []
  10. tries = 9
  11.  
  12. # Статистика (речник)
  13. stats = {
  14.     "опити": tries,
  15.     "грешки": 0,
  16.     "познати_букви": 0
  17. }
  18.  
  19. print("=== Игра на Бесеница ===")
  20.  
  21. while "_" in hidden_letters and tries > 0:
  22.     print("\nДума: ", " ".join(hidden_letters))
  23.     print("Оставащи опити:", tries)
  24.     print("Познати букви:", ", ".join(guessed_letters))
  25.  
  26.     try:
  27.         letter = input("Въведи буква: ").strip().lower()
  28.  
  29.         if not letter.isalpha() or len(letter) != 1:
  30.             raise ValueError("Моля, въведи само една буква.")
  31.  
  32.         if letter in guessed_letters:
  33.             print("Вече си пробвал тази буква.")
  34.             continue
  35.  
  36.         guessed_letters.append(letter)
  37.  
  38.         if letter in chosen_word:
  39.             for i in range(len(chosen_word)):
  40.                 if chosen_word[i] == letter:
  41.                     hidden_letters[i] = letter
  42.                     stats["познати_букви"] += 1
  43.             print("Позна!")
  44.         else:
  45.             tries -= 1
  46.             stats["грешки"] += 1
  47.             print("Грешка!")
  48.  
  49.     except ValueError as ve:
  50.         print("Грешка:", ve)
  51.     except Exception as e:
  52.         print("Възникна неочаквана грешка:", e)
  53.  
  54. # Резултат (tuple)
  55. result = ("Поздравления!" if "_" not in hidden_letters else "Не успя.",
  56.           chosen_word,
  57.           stats["познати_букви"],
  58.           stats["грешки"])
  59.  
  60. print("\n=== Край на играта ===")
  61. print(result[0])
  62. print(f"Думата беше: {result[1]}")
  63. print(f"Познати букви: {result[2]}, Грешки: {result[3]}")
  64.  
  65. # Запис във файл (file handling)
  66. try:
  67.     with open("hangman_results.txt", "a", encoding="utf-8") as file:
  68.         file.write(f"Резултат: {result[0]}, Дума: {result[1]}, Познати: {result[2]}, Грешки: {result[3]}\n")
  69.     print("Резултатът е записан във файл hangman_results.txt")
  70. except Exception:
  71.     print("Грешка при запис във файл.")
  72.  
Advertisement
Add Comment
Please, Sign In to add comment