Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import string
- class Gallows():
- list_of_words = ["python", "java", "swift", "javascript"]
- random_choices = list_of_words[random.randrange(0,4)]
- user_attempts = 8
- a_word_to_guess = list(random_choices)
- list_with_lettrs = ["-" for _ in range(len(a_word_to_guess))]
- marker, users_letter = "", ""
- victory_counterer, loss_counterer = 0, 0
- def game_menu(self):
- user_input = input('Type "play" to play the game, "results" to show the scoreboard, and "exit" to quit: ')
- while user_input != exit:
- if user_input == "play":
- Gallows.game(self)
- elif user_input == "results":
- print("You won: {} times.\nYou lost: {} times.".format(self.victory_counterer, self.loss_counterer))
- def game(self):
- print("H A N G M A N\n")
- while self.user_attempts > 0 :
- print("".join(self.list_with_lettrs))
- user_inp = input("Input a letter: ")
- if len(user_inp) == 1:
- if user_inp not in string.ascii_lowercase:
- print("Please, enter a lowercase letter from the English alphabet.\n")
- continue
- else:
- if user_inp in self.users_letter:
- print("You've already guessed this letter.\n")
- continue
- else:
- print("Please, input a single letter.\n")
- continue
- for chr in range(len(self.a_word_to_guess)):
- if self.a_word_to_guess[chr] == user_inp:
- self.list_with_lettrs[chr] = user_inp
- if self.list_with_lettrs.count(self.list_with_lettrs[chr]) == 1:
- self.marker = "Yes"
- if user_inp not in self.a_word_to_guess:
- print("That letter doesn't appear in the word.\n")
- self.user_attempts -=1
- else:
- print()
- self.users_letter += user_inp
- if self.list_with_lettrs == self.a_word_to_guess:
- print("You guessed the word {}!\nYou survived!".format("".join(self.list_with_lettrs)))
- self.victory_counterer += 1
- Gallows.game_menu(self)
- break
- elif self.user_attempts == 0:
- if self.a_word_to_guess == self.list_with_lettrs:
- print("You guessed the word {}!\nYou survived!".format("".join(self.list_with_lettrs)))
- self.victory_counterer += 1
- Gallows.game_menu(self)
- else:
- self.loss_counterer += 1
- print("You lost!")
- Gallows.game_menu(self)
- obj = Gallows()
- obj.game_menu()
Add Comment
Please, Sign In to add comment