Skip to content

Instantly share code, notes, and snippets.

@tjguk
Created June 22, 2020 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjguk/1aaf115f8b6757e61b439c23d9a15736 to your computer and use it in GitHub Desktop.
Save tjguk/1aaf115f8b6757e61b439c23d9a15736 to your computer and use it in GitHub Desktop.
Guess
import pathlib
import random
#
# Get a list of category filenames
#
category_filenames = pathlib.Path(".").glob("*.txt")
#
# Display each category name in turn
#
print("Categories:")
for filename in category_filenames:
print(filename.stem)
#
# Ask the user to select a category
#
print()
category = input("Choose a category: ")
#
# Pick a random word from that category and remember
# how many letters it has
#
words = open(category + ".txt").read().split()
chosen_word = random.choice(words)
chosen_word_length = len(chosen_word)
#
# Tell the player you've picked a word
#
if category.startswith(("a", "e", "i", "o", "u")):
article = "an"
else:
article = "a"
print("I have chosen %s %s with %s letters" % (article, category[:-1], chosen_word_length))
#
# Keep asking the user to guess until they get the word right
#
while True:
guess = input("Guess %s %s " % (article,category[:-1])).upper()
if guess == chosen_word:
print ("Well done")
break
if guess != chosen_word:
print (" hahaha loser")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment