-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangMan.py
More file actions
157 lines (131 loc) · 3.92 KB
/
hangMan.py
File metadata and controls
157 lines (131 loc) · 3.92 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python3
import random
import os
def checkRepeat(char, full):
foundUsed = False
for i in full:
# print('checking: ', i, 'against', char)
if char == (i or ' '):
print('you cannot enter an empty space or already tried character')
foundUsed = True
return char, full, foundUsed
if foundUsed == False:
full.append(char)
# print('updated list: ', full)
return char, full, foundUsed
def checkHit(guess, secretChars):
guessid=[]
for i in range(len(secretChars)):
if guess == secretChars[i]:
# print('Bingo')
guessid.append(i)
return guessid
def printMask(mask):
for i in mask:
print(i, end=' ')
return
def printHangman(missedGuess):
if missedGuess == 0:
print(' +---+')
print(' |')
print(' |')
print(' |')
print(' =====')
elif missedGuess == 1:
print(' +---+')
print(' O |')
print(' |')
print(' |')
print(' =====')
elif missedGuess == 2:
print(' +---+')
print(' O |')
print(' | |')
print(' |')
print(' =====')
elif missedGuess == 3:
print(' +---+')
print(' O |')
print('/| |')
print(' |')
print(' =====')
elif missedGuess == 4:
print(' +---+')
print(' O |')
print('/|\ |')
print(' |')
print(' =====')
elif missedGuess == 5:
print(' +---+')
print(' O |')
print('/|\ |')
print('/ |')
print(' =====')
elif missedGuess == 6:
print(' +---+')
print(' O |')
print('/|\ |')
print('/ \ |')
print(' =====')
def checkComplete(mask):
for i in mask:
if i == '-':
return False
return True
#print('please enter a secret word.\n')
#secret = input()
#print('Randomly picking a word from Shakespeare, And you have SEVEN chance to guess the letters in the word')
#quote = 'full fathom five thy father lies of his bones are coral made those are pearls that were his eyes nothing of him that doth fade but doth suffer a sea-change into something rich and strange'
#sequence = quote.lower().split()
#secret = random.choice(sequence)
secret = 'here'
secretChars = list(secret)
secretLen = len(secret)
mask = [u'-'] * secretLen
printMask(mask)
print()
#print(secretLetters)
print('please guess a letter')
userGuess = input()
missedGuess=0
alreadyGuessed = []
isRepeat = False
#guess, alreadyGuessed, isRepeat = checkRepeat(userGuess, alreadyGuessed)
#tries = 0
while missedGuess < 5:
guess, alreadyGuessed, isRepeat = checkRepeat(userGuess, alreadyGuessed)
while isRepeat is True:
# os.system('clear')
print('please guess a new letter')
userGuess = input()
guess, alreadyGuessed, isRepeat = checkRepeat(userGuess, alreadyGuessed)
hitResult = checkHit(guess, secretChars)
# print(hitResult)
if hitResult:
print('Bingo, the letter', guess,'is in position', end=' ')
for i in hitResult:
print(i+1, end=' ')
mask[i] = secretChars[i]
print()
printHangman(missedGuess)
printMask(mask)
print()
else:
missedGuess += 1
print('Sorry, you still have', 6-missedGuess,'chance(s).')
printHangman(missedGuess)
printMask(mask)
print()
if checkComplete(mask) == True:
print('Congratulations, you won! The secret is: ', secret)
break
print('please enter next guess')
userGuess = input()
# tries += 1
#print('you guessed', alreadyGuessed, 'and the secret is', secret,'adding a test line for github')
if checkComplete(mask) == False:
print('Sorry you lose! your final result is ', end=' ')
printMask(mask)
print()
printHangman(6)
print('The secret word is :',secret)