Advertisement
jimMoonrock

Game

May 19th, 2022
585
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2. import string
  3.  
  4. class main_func():
  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 = ""
  11.     users_letter = ""
  12.    
  13.    
  14.    
  15.     def game(self):
  16.         print("H A N G M A N\n")
  17.  
  18.         while self.user_attempts > 0 :
  19.    
  20.    
  21.             print("".join(self.list_with_lettrs))
  22.             user_inp = input("Input a letter:   ")
  23.    
  24.    
  25.             if len(user_inp) == 1:
  26.                 if user_inp not in string.ascii_lowercase:
  27.                     print("Please, enter a lowercase letter from the English alphabet\n.")
  28.                     continue
  29.                 else:
  30.                     if user_inp == self.users_letter:
  31.                         print("You've already guessed this letter.\n")
  32.                         continue
  33.             else:
  34.                 print("Please, input a single letter.\n")
  35.                 continue
  36.    
  37.    
  38.             for chr in range(len(self.a_word_to_guess)):
  39.        
  40.                  if self.a_word_to_guess[chr] == user_inp:
  41.                      self.list_with_lettrs[chr] = user_inp
  42.            
  43.                      if self.list_with_lettrs.count(self.list_with_lettrs[chr]) == 1:
  44.                          self.marker = "Yes"
  45.                
  46.                
  47.             if user_inp not in self.a_word_to_guess:
  48.                 print("That letter doesn't appear in the word.\n")
  49.                 self.user_attempts -=1
  50.             else:
  51.                 print()
  52.    
  53.             self.users_letter = user_inp
  54.    
  55.             if self.list_with_lettrs == self.a_word_to_guess:
  56.                 print("You guessed the {}!\nYou survived!".format("".join(self.list_with_lettrs)))
  57.                 break
  58.             elif self.user_attempts == 0:
  59.                 if self.a_word_to_guess == self.list_with_lettrs:
  60.                     print("You guessed the {}!\nYou survived!".format("".join(self.list_with_lettrs)))
  61.                 else:
  62.                     print("You lost!")
  63.  
  64.  
  65. obj = main_func()
  66. obj.game()
Advertisement
RAW Paste Data Copied
Advertisement