-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhangman.py
99 lines (79 loc) · 3.08 KB
/
hangman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import random
from collections import Counter
# Defines list of words used for hangman #
bandList = '''beetles chicago kiss'''
# Splits bandlist so the individual entries
# can be treated separately from each other #
bandList = bandList.split(' ')
# Chooses random word in bandList
# to be used for the game #
word = random.choice(bandList)
if __name__ == '__main__':
print('Guess the rock or metal band!!!')
print()
print('Please use lower case letters only')
# Creates empty word template to convey
# how many entries are in the word #
for i in word:
print('_', end = ' ')
print()
playing = True
letterGuessed = ''
chances = len(word) + 5
correct = 0
flag = 0
try:
# Sets loop and stopping point
# when certain values are reached #
while (chances != 0) and flag == 0:
print()
chances -= 1
# Prompts user to guess a letter #
try:
guess = str(input('Enter a letter to guess: '))
except:
print('Enter only a letter!')
continue
# These provide some handling of invalid inputs #
if not guess.isalpha():
print('Enter only a LETTER')
continue
elif len(guess) > 1:
print('Enter only a SINGLE letter')
continue
elif guess in letterGuessed:
print('You have already guessed that letter')
continue
# If a correct letter is guessed the letter is added
# to the word template displayed on the screen #
if guess in word:
k = word.count(guess)
for _ in range(k):
letterGuessed += guess
# Checks to see if the word has missing
# entries or has been fully guessed #
for char in word:
if char in letterGuessed and (Counter(letterGuessed) != Counter(word)):
print(char, end = ' ')
correct += 1
# If all the words entries have
# been accurately guessed #
elif (Counter(letterGuessed) == Counter(word)):
print("The word is: ", end=' ')
print(word)
flag = 1
print('Congratulations, You won!')
break
break
else:
print('_', end = ' ')
# If the users runs out of guesses and
# has not guessed the full word #
if chances <= 0 and (Counter(letterGuessed) != Counter(word)):
print()
print('You lost! Try again..')
print('The word was {}'.format(word))
except KeyboardInterrupt:
print()
print('Bye! Try again.')
exit()