jimMoonrock

Gallows

May 18th, 2022 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 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.  
  14.  
  15.     def game_menu(self):
  16.         user_input = input('Type "play" to play the game, "results" to show the scoreboard, and "exit" to quit: ')
  17.        
  18.         while  user_input != exit:
  19.             if user_input == "play":
  20.                 Gallows.game(self)
  21.             elif user_input == "results":
  22.                 print("You won: {} times.\nYou lost: {} times.".format(self.victory_counterer,  self.loss_counterer))
  23.            
  24.            
  25.    
  26.     def game(self):
  27.         print("H A N G M A N\n")
  28.        
  29.         while self.user_attempts > 0 :
  30.            
  31.             print("".join(self.list_with_lettrs))
  32.             user_inp = input("Input a letter:   ")
  33.    
  34.    
  35.             if len(user_inp) == 1:
  36.                 if user_inp not in string.ascii_lowercase:
  37.                     print("Please, enter a lowercase letter from the English alphabet.\n")
  38.                     continue
  39.                 else:
  40.                     if user_inp in self.users_letter:
  41.                         print("You've already guessed this letter.\n")
  42.                         continue
  43.             else:
  44.                 print("Please, input a single letter.\n")
  45.                 continue
  46.    
  47.    
  48.             for chr in range(len(self.a_word_to_guess)):
  49.        
  50.                  if self.a_word_to_guess[chr] == user_inp:
  51.                      self.list_with_lettrs[chr] = user_inp
  52.            
  53.                      if self.list_with_lettrs.count(self.list_with_lettrs[chr]) == 1:
  54.                          self.marker = "Yes"
  55.                
  56.                
  57.             if user_inp not in self.a_word_to_guess:
  58.                 print("That letter doesn't appear in the word.\n")
  59.                 self.user_attempts -=1
  60.             else:
  61.                 print()
  62.    
  63.             self.users_letter += user_inp
  64.    
  65.    
  66.             if self.list_with_lettrs == self.a_word_to_guess:
  67.                 print("You guessed the word {}!\nYou survived!".format("".join(self.list_with_lettrs)))
  68.                 self.victory_counterer += 1
  69.                 Gallows.game_menu(self)
  70.                 break
  71.                
  72.             elif self.user_attempts == 0:
  73.                 if self.a_word_to_guess == self.list_with_lettrs:
  74.                     print("You guessed the word {}!\nYou survived!".format("".join(self.list_with_lettrs)))
  75.                     self.victory_counterer += 1
  76.                     Gallows.game_menu(self)
  77.                 else:
  78.                     self.loss_counterer += 1
  79.                     print("You lost!")
  80.                     Gallows.game_menu(self)
  81.                    
  82.  
  83. obj = Gallows()
  84. obj.game_menu()
Add Comment
Please, Sign In to add comment