Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- word = ["APPLE", "BANANA", "CHERRY", "STRAWBERRY", "MANGO"] # list of words to be guessed
- wrongGuess = [] # list of wrong guesses of the player
- visibleWord = ["_"] # list which will be displayed to the player
- attempts = 0
- failAttempts = 0 # number of wrong guesses of the player
- guessed = False
- # this function checks if player already tried this letter
- def already_played(letter):
- if letter in wrongGuess:
- return True
- elif letter in visibleWord:
- return True
- else:
- return False
- # this function lists wrong guesses to make them visible to the player
- def print_wrong_guesses():
- print("Wrong guesses so far: ")
- for x in wrongGuess:
- print(x + " ", end=' ')
- print()
- # this function displays the word to the player to see the positions of guessed letters
- def print_visible_word():
- for symbol in visibleWord:
- print(symbol + " ", end=' ')
- print()
- # this function displays the word to be guessed
- def print_hidden_word():
- for letter in hiddenWord:
- print(letter + " ", end=' ')
- print()
- # this function tells if the word is guessed by the player
- def is_guessed():
- if "_" in visibleWord:
- return False
- else:
- return True
- # this function updates the word displayed to the player when the player guesses a letter
- def update_visible_word():
- counter = 0
- for letter in hiddenWord:
- if letter == guessedLetter:
- visibleWord[counter] = letter
- counter += 1
- # this function prints game hangman picture on the console based on the wrong guesses
- def print_template(wrongs):
- if wrongs == 1:
- for symbol in range(10):
- print("|")
- elif wrongs == 2:
- print("|", end=' ')
- for symbol in range(10):
- print("-", end=' ')
- print()
- for symbol in range(9):
- print("|")
- elif wrongs == 3:
- print("|", end=' ')
- for symbol in range(10):
- print("-", end=' ')
- print()
- print("| |")
- for symbol in range(10):
- print("|")
- elif wrongs == 4:
- print("|", end=' ')
- for symbol in range(10):
- print("-", end=' ')
- print()
- print("| |")
- print("| *** ")
- print("| *****")
- print("| ***")
- for symbol in range(8):
- print("|")
- elif wrongs == 5:
- print("|", end=' ')
- for symbol in range(10):
- print("-", end=' ')
- print()
- print("| |")
- print("| *** ")
- print("| *****")
- print("| ***")
- print("| |")
- print("| *******")
- print("| *******")
- print("| *******")
- print("| *******")
- for symbol in range(7):
- print("|")
- elif wrongs == 6:
- print("|", end=' ')
- for symbol in range(10):
- print("-", end=' ')
- print()
- print("| |")
- print("| *** ")
- print("| *****")
- print("| ***")
- print("| |")
- print("| *******")
- print("| * *******")
- print("| * *******")
- print("| * *******")
- for symbol in range(7):
- print("|")
- elif wrongs == 7:
- print("|", end=' ')
- for symbol in range(10):
- print("-", end=' ')
- print()
- print("| |")
- print("| *** ")
- print("| *****")
- print("| ***")
- print("| |")
- print("| *******")
- print("| * ******* *")
- print("| * ******* *")
- print("| * ******* *")
- for symbol in range(7):
- print("|")
- elif wrongs == 8:
- print("|", end=' ')
- for x in range(10):
- print("-", end=' ')
- print()
- print("| |")
- print("| *** ")
- print("| *****")
- print("| ***")
- print("| |")
- print("| *******")
- print("| * ******* *")
- print("| * ******* *")
- print("| * ******* *")
- print("| *")
- print("| *")
- print("| *")
- print("| *")
- for symbol in range(2):
- print("|")
- elif wrongs == 9:
- print("|", end=' ')
- for symbol in range(10):
- print("-", end=' ')
- print()
- print("| |")
- print("| *** ")
- print("| *****")
- print("| ***")
- print("| |")
- print("| *******")
- print("| * ******* *")
- print("| * ******* *")
- print("| * ******* *")
- print("| * *")
- print("| * *")
- print("| * *")
- print("| * *")
- for symbol in range(2):
- print("|")
- randomWordIndex = random.randint(0, len(word) - 1) # we receive random index for hidden word
- hiddenWord = word[randomWordIndex] # word to be guessed by the player
- lettersCount = len(hiddenWord)
- # we load the hidden word with "_" to show it to the player
- for x in range(len(hiddenWord) - 1):
- visibleWord.append("_")
- # the main logic og the game comes in this loop
- while failAttempts < 9 and guessed is False:
- print()
- print("Can you guess the word?")
- print_visible_word()
- if len(wrongGuess) > 0: # we print the wrongly guessed letters and the template only if any
- print_wrong_guesses()
- print_template(len(wrongGuess))
- print("Please enter a character")
- guessedLetter = input("Your guess is: ").upper()
- if guessedLetter.isalpha():
- if already_played(guessedLetter):
- print("Letter already checked!")
- continue
- if guessedLetter in hiddenWord:
- update_visible_word() # we use global variables here, ask BRANKO if OK!!!
- print("Good guess!")
- else:
- failAttempts += 1
- print("Your letter not in the word!")
- wrongGuess.append(guessedLetter) # we add the wrongly guessed letter in a list
- else:
- print("You entered wrong symbol!")
- guessed = is_guessed()
- if is_guessed():
- print("Congratulations!")
- print_visible_word()
- else:
- print_template(len(wrongGuess))
- print("You are hung!")
- print("The word you could not guess was:")
- print(hiddenWord)
Add Comment
Please, Sign In to add comment