nedelcho

Untitled

Jun 4th, 2022 (edited)
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.65 KB | None | 0 0
  1. import random
  2.  
  3. word = ["APPLE", "BANANA", "CHERRY", "STRAWBERRY", "MANGO"]  # list of words to be guessed
  4. wrongGuess = []  # list of wrong guesses of the player
  5. visibleWord = ["_"]  # list which will be displayed to the player
  6.  
  7. attempts = 0
  8. failAttempts = 0  # number of wrong guesses of the player
  9. guessed = False
  10.  
  11.  
  12. # this function checks if player already tried this letter
  13. def already_played(letter):
  14.     if letter in wrongGuess:
  15.         return True
  16.     elif letter in visibleWord:
  17.         return True
  18.     else:
  19.         return False
  20.  
  21.  
  22. # this function lists wrong guesses to make them visible to the player
  23. def print_wrong_guesses():
  24.     print("Wrong guesses so far: ")
  25.     for x in wrongGuess:
  26.         print(x + " ", end=' ')
  27.     print()
  28.  
  29.  
  30. # this function displays the word to the player to see the positions of guessed letters
  31. def print_visible_word():
  32.     for symbol in visibleWord:
  33.         print(symbol + " ", end=' ')
  34.     print()
  35.  
  36.  
  37. # this function displays the word to be guessed
  38. def print_hidden_word():
  39.     for letter in hiddenWord:
  40.         print(letter + " ", end=' ')
  41.     print()
  42.  
  43.  
  44. # this function tells if the word is guessed by the player
  45. def is_guessed():
  46.     if "_" in visibleWord:
  47.         return False
  48.     else:
  49.         return True
  50.  
  51.  
  52. # this function updates the word displayed to the player when the player guesses a letter
  53. def update_visible_word():
  54.     counter = 0
  55.     for letter in hiddenWord:
  56.         if letter == guessedLetter:
  57.             visibleWord[counter] = letter
  58.         counter += 1
  59.  
  60.  
  61. # this function prints game hangman picture on the console based on the wrong guesses
  62. def print_template(wrongs):
  63.     if wrongs == 1:
  64.         for symbol in range(10):
  65.             print("|")
  66.     elif wrongs == 2:
  67.         print("|", end=' ')
  68.         for symbol in range(10):
  69.             print("-", end=' ')
  70.         print()
  71.         for symbol in range(9):
  72.             print("|")
  73.     elif wrongs == 3:
  74.         print("|", end=' ')
  75.         for symbol in range(10):
  76.             print("-", end=' ')
  77.         print()
  78.         print("|          |")
  79.         for symbol in range(10):
  80.             print("|")
  81.     elif wrongs == 4:
  82.         print("|", end=' ')
  83.         for symbol in range(10):
  84.             print("-", end=' ')
  85.         print()
  86.         print("|          |")
  87.         print("|         *** ")
  88.         print("|        *****")
  89.         print("|         ***")
  90.         for symbol in range(8):
  91.             print("|")
  92.     elif wrongs == 5:
  93.         print("|", end=' ')
  94.         for symbol in range(10):
  95.             print("-", end=' ')
  96.         print()
  97.         print("|          |")
  98.         print("|         *** ")
  99.         print("|        *****")
  100.         print("|         ***")
  101.         print("|          |")
  102.         print("|       *******")
  103.         print("|       *******")
  104.         print("|       *******")
  105.         print("|       *******")
  106.         for symbol in range(7):
  107.             print("|")
  108.     elif wrongs == 6:
  109.         print("|", end=' ')
  110.         for symbol in range(10):
  111.             print("-", end=' ')
  112.         print()
  113.         print("|          |")
  114.         print("|         *** ")
  115.         print("|        *****")
  116.         print("|         ***")
  117.         print("|          |")
  118.         print("|       *******")
  119.         print("|     * *******")
  120.         print("|    *  *******")
  121.         print("|   *   *******")
  122.         for symbol in range(7):
  123.             print("|")
  124.     elif wrongs == 7:
  125.         print("|", end=' ')
  126.         for symbol in range(10):
  127.             print("-", end=' ')
  128.         print()
  129.         print("|          |")
  130.         print("|         *** ")
  131.         print("|        *****")
  132.         print("|         ***")
  133.         print("|          |")
  134.         print("|       *******")
  135.         print("|     * ******* *")
  136.         print("|    *  *******  *")
  137.         print("|   *   *******   *")
  138.         for symbol in range(7):
  139.             print("|")
  140.     elif wrongs == 8:
  141.         print("|", end=' ')
  142.         for x in range(10):
  143.             print("-", end=' ')
  144.         print()
  145.         print("|          |")
  146.         print("|         *** ")
  147.         print("|        *****")
  148.         print("|         ***")
  149.         print("|          |")
  150.         print("|       *******")
  151.         print("|     * ******* *")
  152.         print("|    *  *******  *")
  153.         print("|   *   *******   *")
  154.         print("|         *")
  155.         print("|         *")
  156.         print("|         *")
  157.         print("|         *")
  158.         for symbol in range(2):
  159.             print("|")
  160.     elif wrongs == 9:
  161.         print("|", end=' ')
  162.         for symbol in range(10):
  163.             print("-", end=' ')
  164.         print()
  165.         print("|          |")
  166.         print("|         *** ")
  167.         print("|        *****")
  168.         print("|         ***")
  169.         print("|          |")
  170.         print("|       *******")
  171.         print("|     * ******* *")
  172.         print("|    *  *******  *")
  173.         print("|   *   *******   *")
  174.         print("|         *  *")
  175.         print("|         *  *")
  176.         print("|         *  *")
  177.         print("|         *  *")
  178.         for symbol in range(2):
  179.             print("|")
  180.  
  181.  
  182. randomWordIndex = random.randint(0, len(word) - 1)  # we receive random index for hidden word
  183. hiddenWord = word[randomWordIndex]  # word to be guessed by the player
  184.  
  185. lettersCount = len(hiddenWord)
  186.  
  187. # we load the hidden word with "_" to show it to the player
  188. for x in range(len(hiddenWord) - 1):
  189.     visibleWord.append("_")
  190.  
  191. # the main logic og the game comes in this loop
  192. while failAttempts < 9 and guessed is False:
  193.     print()
  194.     print("Can you guess the word?")
  195.     print_visible_word()
  196.     if len(wrongGuess) > 0:  # we print the wrongly guessed letters and the template only if any
  197.         print_wrong_guesses()
  198.         print_template(len(wrongGuess))
  199.     print("Please enter a character")
  200.     guessedLetter = input("Your guess is: ").upper()
  201.  
  202.     if guessedLetter.isalpha():
  203.         if already_played(guessedLetter):
  204.             print("Letter already checked!")
  205.             continue
  206.         if guessedLetter in hiddenWord:
  207.             update_visible_word()  # we use global variables here, ask BRANKO if OK!!!
  208.             print("Good guess!")
  209.  
  210.         else:
  211.             failAttempts += 1
  212.             print("Your letter not in the word!")
  213.             wrongGuess.append(guessedLetter)  # we add the wrongly guessed letter in a list
  214.  
  215.     else:
  216.         print("You entered wrong symbol!")
  217.     guessed = is_guessed()
  218.  
  219. if is_guessed():
  220.     print("Congratulations!")
  221.     print_visible_word()
  222. else:
  223.     print_template(len(wrongGuess))
  224.     print("You are hung!")
  225.     print("The word you could not guess was:")
  226.     print(hiddenWord)
  227.  
Add Comment
Please, Sign In to add comment