Skip to content

Instantly share code, notes, and snippets.

@dimaqq
Created March 13, 2018 13:56
Show Gist options
  • Save dimaqq/b84bc171ecbe4824f62e89c4795e5b01 to your computer and use it in GitHub Desktop.
Save dimaqq/b84bc171ecbe4824f62e89c4795e5b01 to your computer and use it in GitHub Desktop.
def game():
"""Play a single game, interactively"""
last = None # last word entered by player
seen = set() # all the words player tried so far
while True:
guess = input("Please type a word: ")
if not guess:
raise Exception("That's not fair!")
if guess not in words:
raise Exception("That's not a dictionary word!")
if guess in seen:
raise Exception("That word has been used already!")
if last and guess[0] != last[-1]:
raise Exception("The word should have started with %r" % last[-1])
seen.add(guess)
last = guess
if __name__ == "__main__":
words = set(line.strip().lower() # remove line terminators; accept proper nouns
for line in open("word_list.txt") # read file line by line; each line is a word
if len(line.strip()) > 1) # 1-letter words are not valid
try:
game()
except Exception as e:
print(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment