Created
June 22, 2020 17:35
-
-
Save tjguk/1aaf115f8b6757e61b439c23d9a15736 to your computer and use it in GitHub Desktop.
Guess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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