@@ -5,7 +5,8 @@ def pick_random_word():
5
5
This function picks a random word from the SOWPODS
6
6
dictionary.
7
7
"""
8
- # open the sowpods dictionary
8
+ # open the sowpods dictionary as a text file in readable format
9
+ # for more information on opening files visit https://pythontips.com/2014/01/15/the-open-function-explained/
9
10
with open ("sowpods.txt" , 'r' ) as f :
10
11
words = f .readlines ()
11
12
@@ -14,6 +15,7 @@ def pick_random_word():
14
15
index = random .randint (0 , len (words ) - 1 )
15
16
16
17
# print out the word at that index
18
+ # the .strip() function removes all trailing spaces before and after the word
17
19
word = words [index ].strip ()
18
20
return word
19
21
@@ -30,13 +32,19 @@ def generate_word_string(word, letters_guessed):
30
32
output .append (letter .upper ())
31
33
else :
32
34
output .append ("_" )
35
+
36
+ # creates a string from the members of the list by using whitespace as a separator
33
37
return " " .join (output )
34
38
35
-
39
+ # make sure that the module is being run standalone and not imported by another user
40
+ # visit http://ibiblio.org/g2swap/byteofpython/read/module-name.html for more information
36
41
if __name__ == '__main__' :
37
42
WORD = pick_random_word ()
38
43
44
+ # creates a set containing the letters of WORD
39
45
letters_to_guess = set (WORD )
46
+
47
+ # creates an empty set
40
48
correct_letters_guessed = set ()
41
49
incorrect_letters_guessed = set ()
42
50
num_guesses = 0
@@ -69,7 +77,7 @@ def generate_word_string(word, letters_guessed):
69
77
print (word_string )
70
78
print ("You have {} guesses left" .format (6 - num_guesses ))
71
79
72
- # tell the user they have won or lost
80
+ # tell the user whether they have won or lost
73
81
if num_guesses < 6 :
74
82
print ("Congratulations! You correctly guessed the word {}" .format (WORD ))
75
83
else :
0 commit comments