-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
51 lines (45 loc) · 1.09 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
'''
Really simple hangman game using print and input
'''
import random
file = open('list_of_words.txt')
line = random.randint(1, 1000)
content = file.readlines()
word = content[line]
word = word.rstrip()
letters = []
guessed_letters = []
wrong_letters = []
#print(word)
for x in word:
x.upper()
letters.append(x)
for x in letters:
guessed_letters.append('_')
print(''.join(guessed_letters))
def guessed(letter, word):
letter_count = 0
if letter == word:
print("You have won, congratulations!")
return False
for x in letters:
if letter is x:
guessed_letters[letter_count] = letter
letter_count = letter_count + 1
if letter not in word:
print("Letter not in word")
print("You have", 4 - len(wrong_letters), "guesses remaining")
wrong_letters.append(letter)
while True:
guess = input("Enter guess: ")
guess.upper()
if guessed(guess, word) is False:
break
print(''.join(guessed_letters))
if '_' not in guessed_letters:
print("Congratulations! You have won!")
break
elif len(wrong_letters) == 5:
print("You have hung your man")
print("Word was: {}".format(word))
break