-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangman.py
More file actions
57 lines (51 loc) · 1.66 KB
/
Hangman.py
File metadata and controls
57 lines (51 loc) · 1.66 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
import random
def chosen_word():
words= ['apple','banana','broccoli','cauliflower','beetroot','fox','dog','girrafe']
return random.choice(words).upper()
def check(word,guesses,guess):
#guess=guess.upper()
status = ''
#i=0
matches = 0
for letter in word:
if letter in guesses:
status += letter
else :
status+='*'
if letter == guess:
matches+= 1
if matches > 1:
print("Yes the word contains",matches,'"'+guess+'"'+ 's')
elif matches == 1:
print("Yes the word contains the letter",'"' +guess+'"')
else:
print("Sorry ,The word doesnot contain the letter")
return status
def main():
word = chosen_word()
guesses = []
guessed=False
print('The word contains', len(word),'letters')
while not guessed:
text = 'Please enter one letter or a {}-letter word.'.format(len(word))
guess=input(text)
guess=guess.upper()
if guess in guesses:
print('You have already guessed "' + guess + '"')
elif len(guess) == len(word):
guesses.append(guess)
if guess == word:
guessed = True
else:
print("The guess is incorrect")
elif len(guess) == 1:
guesses.append(guess)
result = check(word,guesses,guess)
if result == word:
guessed = True
else:
print(result)
else:
print('Inavlid Entry')
print("Bingo! You have guessed the word ", word+ 'in' , len(guesses),'tries' )
main()