Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 774e88c

Browse files
Merge pull request #143 from lironmiz/hangmanproject_final
add the final project
2 parents 16993d3 + d9f5f6f commit 774e88c

File tree

1 file changed

+225
-0
lines changed

1 file changed

+225
-0
lines changed

‎hangman_final_project.py

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
import sys
2+
def welcome_Screen():
3+
"""
4+
Print the logo and number of attempts.
5+
6+
:param None
7+
:return: None
8+
:rtype: None
9+
"""
10+
11+
HANGMAN_GAME_LOGO = """
12+
__ __ ___ .__ __. _______ .___ ___. ___ .__ __. _______ ___ .___ ___. _______
13+
| | | | / \ | \ | | / _____|| \/ | / \ | \ | | / _____| / \ | \/ | | ____|
14+
| |__| | / ^ \ | \| | | | __ | \ / | / ^ \ | \| | | | __ / ^ \ | \ / | | |__
15+
| __ | / /_\ \ | . ` | | | |_ | | |\/| | / /_\ \ | . ` | | | |_ | / /_\ \ | |\/| | | __|
16+
| | | | / _____ \ | |\ | | |__| | | | | | / _____ \ | |\ | | |__| | / _____ \ | | | | | |____
17+
|__| |__| /__/ \__\ |__| \__| \______| |__| |__| /__/ \__\ |__| \__| \______| /__/ \__\ |__| |__| |_______|
18+
"""
19+
print(HANGMAN_GAME_LOGO)
20+
MAX_TRIES = 6
21+
print(f"you have {MAX_TRIES} guess")
22+
23+
def choose_word(file_path, index):
24+
"""
25+
the function return the secret word for guessing.
26+
27+
:param file_path: A string representing a path to a text file containing space-separated words
28+
:param index: Position of a particular word in the file
29+
:return: the secret word for guessing
30+
:rtype: str
31+
"""
32+
# open the file at read mode and read the words from the file
33+
while True: # check if user file_path correct
34+
try:
35+
with open(file_path, 'r') as file:
36+
words = file.read().split()
37+
# take the secret word
38+
while True: # check if user index is correct
39+
try:
40+
secret_word = words[(int(index) - 1) % len(words)]
41+
return secret_word
42+
except ValueError:
43+
print(f"sorry, {index} is not a corret index. Please try again!")
44+
sys.exit()
45+
46+
except:
47+
print(f"sorry, {file_path} is not a corret file name. Please try again!")
48+
sys.exit()
49+
50+
def print_hangman(num_of_tries):
51+
"""Prints the value of the key given as paramter.
52+
53+
:param num_of_tries: a key for the dict(HANGMAN_PHOTOS)
54+
:type num_of_tries: int
55+
:return: None
56+
"""
57+
58+
HANGMAN_PHOTOS = {
59+
1: """ x-------x""",
60+
2: """ x-------x
61+
|
62+
|
63+
|
64+
|
65+
|""",
66+
3: """ x-------x
67+
| |
68+
| 0
69+
|
70+
|
71+
|""",
72+
4: """ x-------x
73+
| |
74+
| 0
75+
| |
76+
|
77+
|""",
78+
5: """ x-------x
79+
| |
80+
| 0
81+
| /|\\
82+
|
83+
|""",
84+
6: """ x-------x
85+
| |
86+
| 0
87+
| /|\\
88+
| /
89+
|""",
90+
7: """ x-------x
91+
| |
92+
| 0
93+
| /|\\
94+
| / \\
95+
|"""
96+
}
97+
hangman_status = HANGMAN_PHOTOS[num_of_tries]
98+
print(hangman_status)
99+
100+
def check_valid_input(letter_guessed, old_letters_guessed):
101+
"""
102+
A Boolean function that accepts a character and a list of letters
103+
that the user has guessed previously. The function checks
104+
two things: the correctness of the input and whether it is legal
105+
to guess this letter (that is, the player has not guessed this letter before)
106+
and returns true or false accordingly.
107+
108+
:param letter_guessed: character received from the user.
109+
:type letter_guessed: str
110+
:param old_letters_guessed: list contains the letters the player has guessed.
111+
:type old_letters_guessed: list
112+
:return: return True if it is a legal input, else return False
113+
:rtype: bool
114+
"""
115+
# if the character is not valid return false
116+
if letter_guessed.isalpha() != True or len(letter_guessed) > 1:
117+
return False
118+
elif letter_guessed in old_letters_guessed: # if the character is not in the letters the player has guessed we return false
119+
return False
120+
else:
121+
return True
122+
123+
def try_update_letter_guessed(letter_guessed, old_letters_guessed):
124+
"""
125+
The method checks if the character is correct
126+
and if it is correct and does not exist in
127+
the letters that the user guessed then it updates the list with
128+
the new character and returns true otherwise it returns false
129+
130+
:param letter_guessed: character received from the user.
131+
:type letter_guessed: str
132+
:param old_letters_guessed: list contains the letters the player has guessed.
133+
:type old_letters_guessed: list
134+
:return: return True if it is a legal input, else return False
135+
:rtype: bool
136+
"""
137+
# check if the input is valid
138+
if check_valid_input(letter_guessed, old_letters_guessed) == False:
139+
print('X')
140+
old_letters_guessed = ' -> '.join(sorted(old_letters_guessed))
141+
print(old_letters_guessed)
142+
return False
143+
else:
144+
old_letters_guessed.append(letter_guessed)
145+
return True
146+
147+
def show_hidden_word(secret_word, old_letters_guessed):
148+
"""
149+
A function that returns a string consisting of letters and underscores.
150+
151+
:param secret_word: word the user has to guess
152+
:type secret_word: str
153+
:param old_letters_guessed: list contains the letters the player has guessed.
154+
:type old_letters_guessed: str
155+
:return: reveal the secret word to the player in a lower-line structure
156+
:rtype: str
157+
"""
158+
# we making the result string by loop all the letters in the secret word
159+
# and if the letter in the letters the user has guessed we add the letter to the result
160+
# and if not we and _ to the result
161+
result = ''
162+
for letter in secret_word:
163+
if letter in old_letters_guessed:
164+
result += letter + ' '
165+
else:
166+
result += '_ '
167+
return result
168+
169+
def check_win(secret_word, old_letters_guessed):
170+
"""
171+
This is a boolean function that returns true if all the letters that make up the secret word
172+
are included in the list of letters that the user guessed. Otherwise, the function returns false.
173+
174+
:param secret_word: word the user has to guess
175+
:param old_letters_guessed: list contains the letters the player has guessed.
176+
:type secret_word: str
177+
:type old_letters_guessed: list
178+
:return: true if all the letters that make up the secret word are included int the list of old_letters_guessed otherwise return false
179+
:rtype: bool
180+
"""
181+
# looping all the letters in secret word if one of them not int the guessed word we return False otherwise we return True
182+
for letter in secret_word:
183+
if letter not in old_letters_guessed:
184+
return False
185+
return True
186+
187+
def main():
188+
189+
# print the welcome screen
190+
welcome_Screen()
191+
wrong_guess = 0
192+
num_of_tries = 1
193+
old_letters_guessed = list() # create an empty list for the beginning
194+
# take from user the file path and index
195+
file_path = input("Enter file path: ")
196+
index = input("Enter index: ")
197+
secret_word = choose_word(file_path, index)
198+
print("Let’s start!")
199+
print_hangman(num_of_tries)
200+
print("_ " * len(secret_word))
201+
# game loop
202+
while wrong_guess in range(6):
203+
# take user input
204+
guessed_letter = input("Guess a letter: ").lower()
205+
# the main logic of the game
206+
if try_update_letter_guessed(guessed_letter, old_letters_guessed):
207+
if guessed_letter not in secret_word:
208+
wrong_guess += 1
209+
num_of_tries += 1
210+
print(":(")
211+
print_hangman(num_of_tries)
212+
print(show_hidden_word(secret_word, old_letters_guessed))
213+
else:
214+
print(show_hidden_word(secret_word, old_letters_guessed))
215+
if check_win(secret_word, old_letters_guessed):
216+
print("WIN")
217+
break
218+
if wrong_guess == 6:
219+
if check_win(secret_word, old_letters_guessed):
220+
print("WIN")
221+
else:
222+
print("LOSE")
223+
224+
if __name__ == "__main__":
225+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /