-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
136 lines (104 loc) · 3.77 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
130
131
132
133
134
135
136
import string
from words import choose_word
from images import IMAGES
'''
Important instruction
* function and variable name snake_case -> is_prime
* contant variable upper case PI
'''
def is_word_guessed(secret_word, letters_guessed):
'''
secret_word: word guess by the user
letters_guessed: list hold all the word guess by the user
returns:
return True (if user guess the world correctly )
return False (wrong selection)
'''
m=''
for i in letters_guessed:
if i in secret_word:
m=m+i
if m==secret_word :
return True
else:
return False
# if you want to test this function please call function -> get_guessed_word("kindness", [k, n, d])
def get_guessed_word(secret_word, letters_guessed):
'''
secret_word: word guess by the user
letters_guessed: list hold all the word guess by the user
returns:
return string which contain all the right guessed characters
Example :-
if secret_word -> "kindness" and letters_guessed = [k, n, s]
return "k_n_n_ss"
'''
list1=[]
for i in secret_word:
if i in letters_guessed:
list1.append(i)
index = 0
guessed_word = ''
while (index < len(secret_word)):
if secret_word[index] in letters_guessed:
guessed_word += secret_word[index]
else:
guessed_word += "_"
index += 1
return guessed_word
def get_available_letters(letters_guessed):
'''
letters_guessed: list contains all guessed characters
returns:
it return string which contains all characters except guessed letters
Example :-
letters_guessed = ['e', 'a'] then
return sting is -> `bcdfghijklmnopqrstuvwxyz`
'''
letters_left = string.ascii_lowercase
letters_left=[i for i in letters_left if i not in letters_guessed]
return ''.join(letters_left)
def images(num):
return IMAGES[num]
def hangman(secret_word):
'''
secret_word (string) : secret word to guessed by the user.
Steps to start Hangman:
* In the beginning of the game user will know about the total characters in the secret_word
* In each round user will guess one character
* After each character give feedback to the user
* right or wrong
* Display partial word guessed by the user and use underscore in place of not guess word
'''
print("Welcome to the game, Hangman!")
print("I am thinking of a word that is {} letters long.".format(
str(len(secret_word))), end='\n\n')
letters_guessed = []
wrong=8
while wrong>0:
available_letters = get_available_letters(letters_guessed)
print("Available letters: {} ".format(available_letters))
print("Remaining lives :" + str(wrong))
guess = input("Please guess a letter: ")
letter = guess.lower()
if letter in secret_word:
letters_guessed.append(letter)
print("Good guess: {} ".format(
get_guessed_word(secret_word, letters_guessed)))
if is_word_guessed(secret_word, letters_guessed) == True:
print(" * * Congratulations, you won! * * ", end='\n\n')
break
else :
print("Oops! That letter is not in my word: {} ".format(
get_guessed_word(secret_word, letters_guessed)))
image=images(8-wrong)
print (image)
if(wrong==1):
print("You Lose !!!"+"The word was "+secret_word)
wrong=wrong-1
letters_guessed.append(letter)
print("")
# Load the list of words into the variable wordlist
# So that it can be accessed from anywhere in the program
secret_word = choose_word()
hangman(secret_word)