|
| 1 | +# Python Program to illustrate |
| 2 | +# Hangman Game |
| 3 | +import random |
| 4 | +from collections import Counter |
| 5 | + |
| 6 | +someWords = '''apple banana mango strawberry |
| 7 | +orange grape pineapple apricot lemon coconut watermelon |
| 8 | +cherry papaya berry peach lychee muskmelon''' |
| 9 | + |
| 10 | +someWords = someWords.split(' ') |
| 11 | +# randomly choose a secret word from our "someWords" LIST. |
| 12 | +word = random.choice(someWords) |
| 13 | + |
| 14 | +if __name__ == '__main__': |
| 15 | + print('Guess the word! HINT: word is a name of a fruit') |
| 16 | + |
| 17 | + for i in word: |
| 18 | + # For printing the empty spaces for letters of the word |
| 19 | + print('_', end=' ') |
| 20 | + print() |
| 21 | + |
| 22 | + playing = True |
| 23 | + # list for storing the letters guessed by the player |
| 24 | + letterGuessed = '' |
| 25 | + chances = len(word) + 2 |
| 26 | + correct = 0 |
| 27 | + flag = 0 |
| 28 | + |
| 29 | + try: |
| 30 | + while (chances != 0) and flag == 0: # flag is updated when the word is correctly guessed |
| 31 | + print() |
| 32 | + chances -= 1 |
| 33 | + |
| 34 | + try: |
| 35 | + guess = str(input('Enter a letter to guess: ')) |
| 36 | + except: |
| 37 | + print('Enter only a letter!') |
| 38 | + continue |
| 39 | + |
| 40 | + # Validation of the guess |
| 41 | + if not guess.isalpha(): |
| 42 | + print('Enter only a LETTER') |
| 43 | + continue |
| 44 | + elif len(guess) > 1: |
| 45 | + print('Enter only a SINGLE letter') |
| 46 | + continue |
| 47 | + elif guess in letterGuessed: |
| 48 | + print('You have already guessed that letter') |
| 49 | + continue |
| 50 | + |
| 51 | + # If letter is guessed correctly |
| 52 | + if guess in word: |
| 53 | + # k stores the number of times the guessed letter occurs in the word |
| 54 | + k = word.count(guess) |
| 55 | + for _ in range(k): |
| 56 | + letterGuessed += guess # The guess letter is added as many times as it occurs |
| 57 | + |
| 58 | + # Print the word |
| 59 | + for char in word: |
| 60 | + if char in letterGuessed and (Counter(letterGuessed) != Counter(word)): |
| 61 | + print(char, end=' ') |
| 62 | + correct += 1 |
| 63 | + # If user has guessed all the letters |
| 64 | + # Once the correct word is guessed fully, |
| 65 | + elif (Counter(letterGuessed) == Counter(word)): |
| 66 | + # the game ends, even if chances remain |
| 67 | + print("The word is:", end=' ') |
| 68 | + print(word) |
| 69 | + flag = 1 |
| 70 | + print('Congratulations, You won!') |
| 71 | + break # To break out of the for loop |
| 72 | + break # To break out of the while loop |
| 73 | + else: |
| 74 | + print('_', end=' ') |
| 75 | + |
| 76 | + # If user has used all of his chances |
| 77 | + if chances <= 0 and (Counter(letterGuessed) != Counter(word)): |
| 78 | + print() |
| 79 | + print('You lost! Try again..') |
| 80 | + print('The word was {}'.format(word)) |
| 81 | + |
| 82 | + except KeyboardInterrupt: |
| 83 | + print() |
| 84 | + print('Bye! Try again.') |
| 85 | + exit() |
0 commit comments