-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangman.py
129 lines (118 loc) · 4.48 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"This is my new Hangman game."
import random
Word_File = "words.txt"
def loadwords():
OpenText = open(Word_File, 'r')
ReadText = OpenText.readline()
LineText = ReadText.split()
return LineText
def chooseword(WordList):
return random.choice(WordList)
def wordlength(secretword):
length = len(secretword)
return print("I'm thinking of a word that has", length, "letters.")
def iswordguessed(secretword, lettersguessed):
letters = secretword.split()
for letters in secretword:
if not letters in lettersguessed:
return False
return True
def availableletters(lettersguessed):
availableletters = "abcdefghijklmnopqrstuvwxyz"
result = ""
for i in availableletters:
if not i in lettersguessed:
result += i
availableletters = result
return print("Availble letters:",availableletters)
def getguessedword(secretword, lettersguessed):
hidden = list(secretword)
for i in range(len(hidden)):
if not hidden[i] in lettersguessed:
hidden[i]='_ '
return print(' '.join(hidden))
def hangman(secretword, lettersguessed):
print ("Let's play Hangman!")
print(" ")
wordlength(secretword)
GuessCount = 8
print ("You have", GuessCount, "guesses.")
print(" ")
getguessedword(secretword, lettersguessed)
print('')
while GuessCount > 0:
availableletters(lettersguessed)
guess = input("Please choose a letter:")
print("")
print("")
if not guess in secretword:
if not guess in lettersguessed:
GuessCount += -1
print(" ")
print ("Sorry,", guess," is not in my word.")
print ("You have", GuessCount, "guesses left. Try again:")
print(" ")
getguessedword(secretword, lettersguessed)
print(" ")
lettersguessed.append(guess)
else:
print(" ")
print("You already guessed that letter. Try again:")
print(" ")
getguessedword(secretword, lettersguessed)
print(" ")
lettersguessed.append(guess)
if GuessCount == 0:
print("You lost!")
print("The word was", secretword)
print(" ")
replay = input("Would you like to play again?")
print(" ")
if replay == "yes":
print(" ")
lettersguessed = []
WordList = loadwords()
secretword = chooseword(WordList).lower()
hangman(secretword, lettersguessed)
else:
print("Thanks for playing. Goodbye!")
print(" ")
break
else:
if not guess in lettersguessed:
print("")
print("Great guess,", guess, "is in my word!")
lettersguessed.append(guess)
if iswordguessed(secretword,lettersguessed)== True:
print(secretword)
print ("Congratulations, you win!")
print(" ")
replay = input("Would you like to play again?")
print(" ")
if replay == "yes":
print(" ")
lettersguessed = []
WordList = loadwords()
secretword = chooseword(WordList).lower()
hangman(secretword, lettersguessed)
print(" ")
else:
print("Thanks for playing. Goodbye!")
print(" ")
break
else:
print ("You still have", GuessCount, "guesses left.")
print(" ")
getguessedword(secretword, lettersguessed)
print(" ")
else:
print(" ")
print("You already guessed that letter. Try again:")
print(" ")
getguessedword(secretword, lettersguessed)
print(" ")
lettersguessed.append(guess)
WordList = loadwords()
lettersguessed = []
secretword = chooseword(WordList).lower()
hangman(secretword, lettersguessed)