Skip to content

Commit 1694bf5

Browse files
authored
Create hangmanproject_unit9.py
1 parent b91c54e commit 1694bf5

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

hangmanproject_unit9.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# hangman project - unit 9 exercise.
2+
# author - lirom mizrahi
3+
'''
4+
This task will choose for the player a word that will be the secret word for the guess,
5+
from a text file containing a list of words separated by spaces.
6+
7+
Write a function called choose_word defined as follows:
8+
9+
def choose_word(file_path, index):
10+
The function accepts as parameters:
11+
12+
A string (file_path) representing a path to the text file.
13+
An integer (index) representing the position of a certain word in the file.
14+
The function returns a tuple consisting of two members in the following order:
15+
16+
The number of different words in the file, i.e. not including repeated words.
17+
A word in the position received as an argument to the function (index), which will be used as the secret word for guessing.
18+
Guidelines
19+
Treat the positions the player enters as starting from 1 (rather than zero).
20+
If the position (index) is greater than the number of words in the file, the function
21+
continues to count positions in a circular fashion (that is, returns to the first position
22+
in the original list of words in the file and God forbid).
23+
An example of a text file that contains a list of words, called words.txt
24+
hangman song most broadly is a song hangman work music work broadly is typically
25+
Examples of running the choose_word function with the words.txt file
26+
>>> choose_word(r"c:\words.txt", 3)
27+
(9, 'most')
28+
>>> choose_word(r"c:\words.txt", 15)
29+
(9, 'hangman')
30+
31+
'''
32+
33+
def choose_word(file_path, index):
34+
with open(file_path, 'r') as f:
35+
words = f.read().split()
36+
37+
num_unique_words = len(set(words))
38+
39+
secret_word = words[(index - 1) % len(words)]
40+
41+
return (num_unique_words, secret_word)
42+
43+
def main():
44+
result = choose_word(r"words.txt", 3)
45+
print(result)
46+
result = choose_word(r"words.txt", 15)
47+
print(result)
48+
49+
if __name__ == "__main__":
50+
main()

0 commit comments

Comments
 (0)