Skip to content

Instantly share code, notes, and snippets.

@tedmiston
Created January 29, 2022 04:55
Show Gist options
  • Save tedmiston/4de8fafbf111d8c3758ede77a9e7e62d to your computer and use it in GitHub Desktop.
Save tedmiston/4de8fafbf111d8c3758ede77a9e7e62d to your computer and use it in GitHub Desktop.
wordle
import random
import urllib.request
WORD_LENGTH = 5
MAX_GUESSES = 5
WORD_LIST = 'https://raw.githubusercontent.com/tabatkins/wordle-list/main/words'
FORFEIT = 'forfeit'
response = urllib.request.urlopen(WORD_LIST)
content = response.read().decode('utf-8')
all_words = [x for x in content.split('\n') if len(x) == WORD_LENGTH]
print('== wordle.py ==')
random.seed()
random.shuffle(all_words)
the_word = all_words[0]
win = False
guesses_remaining = MAX_GUESSES
while True:
while True:
n = MAX_GUESSES - guesses_remaining + 1
print(f'\n* guess {n}/{MAX_GUESSES}')
guess = input('guess a word: ').lower().strip()
if guess == FORFEIT:
guesses_remaining = 0
break
elif len(guess) != WORD_LENGTH:
print(f'guess is not {WORD_LENGTH} chars!')
elif guess in all_words:
guesses_remaining -= 1
print()
break
else:
print('guess is not in the dictionary!\n')
if guess == the_word:
win = True
break
else:
if guess == FORFEIT:
break
for idx, i in enumerate(guess):
status = '-'
if i in the_word:
status = '🟨'
if the_word[idx] == i:
status = '🟩'
print(f'{i}: {status}')
if not guesses_remaining:
break
if win:
print('*WINNER*!')
else:
print(f'you lose! it was "{the_word}".')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment