Skip to content

Added documentation to the code for Hangman #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Hangman/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ def pick_random_word():
This function picks a random word from the SOWPODS
dictionary.
"""
# open the sowpods dictionary
# open the sowpods dictionary as a text file in readable format
# for more information on opening files visit https://pythontips.com/2014/01/15/the-open-function-explained/
with open("sowpods.txt", 'r') as f:
words = f.readlines()

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

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

Expand All @@ -30,13 +32,19 @@ def generate_word_string(word, letters_guessed):
output.append(letter.upper())
else:
output.append("_")

# creates a string from the members of the list by using whitespace as a separator
return " ".join(output)


# make sure that the module is being run standalone and not imported by another user
# visit http://ibiblio.org/g2swap/byteofpython/read/module-name.html for more information
if __name__ == '__main__':
WORD = pick_random_word()

# creates a set containing the letters of WORD
letters_to_guess = set(WORD)

# creates an empty set
correct_letters_guessed = set()
incorrect_letters_guessed = set()
num_guesses = 0
Expand Down Expand Up @@ -69,7 +77,7 @@ def generate_word_string(word, letters_guessed):
print(word_string)
print("You have {} guesses left".format(6 - num_guesses))

# tell the user they have won or lost
# tell the user whether they have won or lost
if num_guesses < 6:
print("Congratulations! You correctly guessed the word {}".format(WORD))
else:
Expand Down