-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
102 lines (78 loc) · 3.13 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
100
101
102
# import random
# import string
# from words import words
# def get_valid_word(words):
# word = random.choice(words)
# while '-' in word or ' ' in word:
# word = random.choice(words)
# return word
# def hangman():
# word = get_valid_word(words)
# print(word)
# word_letters = set(word)
# alphabet = set(string.ascii_uppercase)
# used_letters = set()
# while len(word_letters) > 0:
# print('You have used these letters:', ' '.join(used_letters))
# word_list = [letter if letter in used_letters else '-' for letter in word]
# print('Current word: ', ' '.join(word_list))
# print(word_list)
# user_letter = input('Guess a letter ').upper()
# if user_letter in alphabet - used_letters:
# used_letters.add(user_letter)
# if user_letter in word_letters:
# word_letters.remove(user_letter)
# print('')
# print(f'word letters {word_letters}')
# elif user_letter in used_letters:
# print('You have already used that character. Please try again.')
# else:
# print('Invalid character. Please try again')
# print(f'used letters {used_letters}')
# if __name__ == '__main__':
# hangman()
import random
from words import words
from hangman_visual import lives_visual_dict
import string
def get_valid_word(words):
word = random.choice(words) # randomly chooses something from the list
while '-' in word or ' ' in word:
word = random.choice(words)
return word.upper()
def hangman():
word = get_valid_word(words)
word_letters = set(word) # letters in the word
alphabet = set(string.ascii_uppercase)
used_letters = set() # what the user has guessed
lives = 7
# getting user input
while len(word_letters) > 0 and lives > 0:
# letters used
# ' '.join(['a', 'b', 'cd']) --> 'a b cd'
print('You have', lives, 'lives left and you have used these letters: ', ' '.join(used_letters))
# what current word is (ie W - R D)
word_list = [letter if letter in used_letters else '-' for letter in word]
print(lives_visual_dict[lives])
print('Current word: ', ' '.join(word_list))
user_letter = input('Guess a letter: ').upper()
if user_letter in alphabet - used_letters:
used_letters.add(user_letter)
if user_letter in word_letters:
word_letters.remove(user_letter)
print('')
else:
lives = lives - 1 # takes away a life if wrong
print('\nYour letter,', user_letter, 'is not in the word.')
elif user_letter in used_letters:
print('\nYou have already used that letter. Guess another letter.')
else:
print('\nThat is not a valid letter.')
# gets here when len(word_letters) == 0 OR when lives == 0
if lives == 0:
print(lives_visual_dict[lives])
print('You died, sorry. The word was', word)
else:
print('YAY! You guessed the word', word, '!!')
if __name__ == '__main__':
hangman()