Created
August 3, 2020 08:00
-
-
Save seowcy/2e35d6c015fea01fed2fea8ee265dc35 to your computer and use it in GitHub Desktop.
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 sys | |
import random | |
DEFAULT_WORDLIST = "wordlist_10000.txt" | |
def load_wordlist(filename): | |
try: | |
with open(filename, 'r') as f: | |
wordlist = [word.strip().lower() for word in f.read().split('\n') if len(word.strip().lower()) in [8, 9, 10]] | |
except FileNotFoundError: | |
print("Wordlist %s not found." % filename) | |
sys.exit(-1) | |
return wordlist | |
def get_word(wordlist): | |
''' | |
Returns a randomly chosen 8, 9, or 10 letter word from a wordlist. | |
''' | |
return random.choice(wordlist) | |
def main(wordlist): | |
word = get_word(wordlist) | |
jumble = list(word) | |
random.shuffle(jumble) | |
jumble = ''.join(jumble) | |
guess = jumble | |
attempts_left = 3 | |
print("Jumbled Word: %s" % jumble) | |
while (guess != word) and (attempts_left > 0): | |
if attempts_left != 3: | |
print("\nIncorrect! ", end='') | |
print("Attempts Left: %s" % attempts_left) | |
guess = input("Enter your guess attempt: ").lower() | |
attempts_left -= 1 | |
if guess == word: | |
print("\nCorrect! The word is %s!" % word) | |
else: | |
print("\nIncorrect! The correct word is %s." % word) | |
sys.exit(0) | |
if __name__ == "__main__": | |
try: | |
assert sys.version_info >= (3, 0) | |
except AssertionError: | |
print("Please rerun the program in Python 3.x (Current version: %s)" % sys.version.split()[0]) | |
sys.exit(-1) | |
args = sys.argv[1:] | |
if len(args) > 1: | |
print("Usage: %s [wordlist_filepath]" % sys.argv[0]) | |
sys.exit(0) | |
elif len(args) == 0: | |
wordlist = load_wordlist(DEFAULT_WORDLIST) | |
else: | |
wordlist = load_wordlist(args[0]) | |
main(wordlist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment