jimMoonrock

Gal

May 19th, 2022 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. import random
  2. import string
  3.  
  4. class Gallows():
  5.     list_of_words = ["python", "java",  "swift", "javascript"]
  6.     random_choices = list_of_words[random.randrange(0,4)]
  7.     user_attempts = 8
  8.     a_word_to_guess = list(random_choices)
  9.     list_with_lettrs = ["-" for _ in range(len(a_word_to_guess))]
  10.     marker, users_letter = "", ""
  11.     victory_counterer, loss_counterer = 0, 0
  12.  
  13.     def game_menu(self):
  14.         user_input = input('Type "play" to play the game, "results" to show the scoreboard, and "exit" to quit: ')
  15.         while  user_input != exit:
  16.             if user_input == "play":
  17.                 self.game()
  18.             elif user_input == "results":
  19.                 print("You won: {} times.\nYou lost: {} times.".format(self.victory_counterer,  self.loss_counterer))
  20.            
  21.     def game(self):
  22.         print("H A N G M A N\n")
  23.        
  24.         while self.user_attempts > 0 :
  25.             print("".join(self.list_with_lettrs))
  26.             user_inp = input("Input a letter:   ")
  27.    
  28.             if len(user_inp) == 1:
  29.                 if user_inp not in string.ascii_lowercase:
  30.                     print("Please, enter a lowercase letter from the English alphabet.\n")
  31.                     continue
  32.                 else:
  33.                     if user_inp in self.users_letter:
  34.                         print("You've already guessed this letter.\n")
  35.                         continue
  36.             else:
  37.                 print("Please, input a single letter.\n")
  38.                 continue
  39.    
  40.             for chr in range(len(self.a_word_to_guess)):
  41.                  if self.a_word_to_guess[chr] == user_inp:
  42.                      self.list_with_lettrs[chr] = user_inp
  43.                      if self.list_with_lettrs.count(self.list_with_lettrs[chr]) == 1:
  44.                          self.marker = "Yes"
  45.                
  46.             if user_inp not in self.a_word_to_guess:
  47.                 print("That letter doesn't appear in the word.\n")
  48.                 self.user_attempts -=1
  49.             else:
  50.                 print()
  51.    
  52.             self.users_letter += user_inp
  53.    
  54.             if self.list_with_lettrs == self.a_word_to_guess:
  55.                 print("You guessed the word {}!\nYou survived!".format("".join(self.list_with_lettrs)))
  56.                 self.victory_counterer += 1
  57.                 self.game_menu()
  58.                 break
  59.        
  60.             elif self.user_attempts == 0:
  61.                 if self.a_word_to_guess == self.list_with_lettrs:
  62.                     print("You guessed the word {}!\nYou survived!".format("".join(self.list_with_lettrs)))
  63.                     self.victory_counterer += 1
  64.                     self.game_menu()
  65.                 else:
  66.                     self.loss_counterer += 1
  67.                     print("You lost!")
  68.                     self.game_menu()
  69.                    
  70. obj = Gallows()
  71. obj.game_menu()
Add Comment
Please, Sign In to add comment