Skip to content

Commit 477eec3

Browse files
authored
Merge pull request #69 from Rijul1999/master
Added documentation to the code for Hangman
2 parents ab01588 + 5f1aed9 commit 477eec3

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

Hangman/code.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ def pick_random_word():
55
This function picks a random word from the SOWPODS
66
dictionary.
77
"""
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/
910
with open("sowpods.txt", 'r') as f:
1011
words = f.readlines()
1112

@@ -14,6 +15,7 @@ def pick_random_word():
1415
index = random.randint(0, len(words) - 1)
1516

1617
# print out the word at that index
18+
# the .strip() function removes all trailing spaces before and after the word
1719
word = words[index].strip()
1820
return word
1921

@@ -30,13 +32,19 @@ def generate_word_string(word, letters_guessed):
3032
output.append(letter.upper())
3133
else:
3234
output.append("_")
35+
36+
# creates a string from the members of the list by using whitespace as a separator
3337
return " ".join(output)
3438

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
3641
if __name__ == '__main__':
3742
WORD = pick_random_word()
3843

44+
# creates a set containing the letters of WORD
3945
letters_to_guess = set(WORD)
46+
47+
# creates an empty set
4048
correct_letters_guessed = set()
4149
incorrect_letters_guessed = set()
4250
num_guesses = 0
@@ -69,7 +77,7 @@ def generate_word_string(word, letters_guessed):
6977
print(word_string)
7078
print("You have {} guesses left".format(6 - num_guesses))
7179

72-
# tell the user they have won or lost
80+
# tell the user whether they have won or lost
7381
if num_guesses < 6:
7482
print("Congratulations! You correctly guessed the word {}".format(WORD))
7583
else:

0 commit comments

Comments
 (0)