-
Notifications
You must be signed in to change notification settings - Fork 33
/
hangman.py
100 lines (81 loc) · 3.02 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
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)
'''
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"
'''
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
return letters_left
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 = []
available_letters = get_available_letters(letters_guessed)
print("Available letters: {} ".format(available_letters))
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')
else:
print("Oops! That letter is not in my word: {} ".format(
get_guessed_word(secret_word, letters_guessed)))
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)