-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.py
More file actions
45 lines (39 loc) · 1.08 KB
/
hangman.py
File metadata and controls
45 lines (39 loc) · 1.08 KB
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
import random
LIVES = 6
def ChooseWord():
words = {"Apple":"Fruit", "Truck":"Vehicle", "Purple":"Color"}
word = random.choice(list(words.keys()))
return word.lower(), words[word]
def CreateWordGuessed(word):
wordList = []
for i in range(len(word)):
wordList.append('-')
return wordList
def GetLetterFromUser():
letter = input("Pick Letter: ").lower()
while len(letter) != 1:
print("Invalid input, please try again...")
letter = input("Pick Letter: ").lower()
return letter
def FitLetterInWordGuessed(word, letter, wordGuessed):
match = False
for i in range(len(word)):
if word[i] == letter:
wordGuessed[i] = letter
match = True
return wordGuessed
def main():
word, wordHint = ChooseWord()
print(word)
wordGuessed = CreateWordGuessed(word)
counter = 0
while True:
print("".join(wordGuessed)+" (hint:"+wordHint+")")
letter = GetLetterFromUser()
wordGuessed, match = FitLetterInWordGuessed(word, letter, wordGuessed)
if match == False:
counter += 1
if counter == LIVES:
print("Game Over!")
break
main()