-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py~
88 lines (66 loc) · 2.21 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
import random
def secret_word():
wordfile = "/usr/share/dict/words"
min_length = 6
good_words = []
f = open(wordfile)
for i in f:
word = i.strip()
if word.isalpha() and len(word) >= min_length:
good_words.append(word)
f.close()
return random.choice(good_words)
#print (secret_word().lower())
def letter_check(w, uinput):
letter_indices = []
number = w.count(uinput)
for i in range(1, number+1):
letter_index = w.index(uinput)
letter_indices.append(letter_index)
w[letter_index] = '-'
return letter_indices
def game():
tries_left = 6
word = list(secret_word().lower())
correct = ''.join(word)
print(correct)
guessed = []
wrong_guesses = []
for i in word:
guessed.append('_')
while tries_left:
user_input = input("\n\nGuess a letter > ")
if len(user_input) != 1:
print("Please enter a single letter")
continue
if user_input in word:
letter_indices = list(letter_check(word, user_input))
for i in letter_indices:
# print(word)
# print(guessed)
guessed[i] = user_input
guess_word = ''.join(guessed)
word[i] = '_'
#print(word)
#print (guessed)
if guess_word == correct:
print("\n",guess_word)
print("You have won")
exit()
print(guess_word)
print("Wrong guesses are: ",' '.join(wrong_guesses))
else:
print("\nWrong")
print(' '.join(guessed))
if user_input not in wrong_guesses:
wrong_guesses.append(user_input)
print("Wrong guesses are: ",' '.join(wrong_guesses))
else:
print("Wrong guesses are: ",' '.join(wrong_guesses))
if tries_left == 1:
print("The right word is ", correct)
print("You have been hanged.")
exit()
else:
tries_left -= 1
print("lifes left: ", tries_left)