Skip to main content
Code Review

Return to Question

Notice removed Content dispute by Community Bot
Post Unlocked by Community Bot
Post Locked by Mast
Notice added Content dispute by Mast
Notice removed Obsolete by Mast
Post Unlocked by Mast
Post Locked by Mast
Notice added Obsolete by Mast
Rollback to Revision 4
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127

NLTK needs to be installed to use the code.

Link to custom word file to supplement words in NLTK dictionary: custom.txt

Link to custom word file of 5-letter words only: custom_5_Letters

Change Log as per "theonlygusti" suggestions:

  • Created custom word files from NLTK dictionaries. So, no need for NLTK dependency. Makes the program run faster.
  • Fixed the error in the keyboard display of words already guessed.
  • Created a global constant for word length.
  • Created two files for differing word lengths. If using 5-letter words only replace:
set_of_words =import open('custom.txt').read().split()time
words = [w for w in set_of_words if len(w) ==from WORD_LENGTH]
wordnltk.corpus =import (random.choice(words)).lower()

with

words =from list(open('custom_5_letters.txt').read()nltk.split())
word =corpus (random.choice(words)).lower()
import timewordnet 
import random
WORD_LENGTH = 5
def Occurences(s, ch):
 return([i for i, letter in enumerate(s) if letter == ch])
def printTile(tile, guess):
 for i in range(len(guess)0,5):
 color = 44 - tile[i]
 print(f'\x1b[1;{color};30m {guess[i].upper()} \x1b[0m', end=' ')
 time.sleep(0.6)
 print(flush=True)
 
def printKeyboard(tile, guess, dic):
 print()
 for key in dic:
 if dic[key] != 0:
 dic[key] = -3
 for i in range(len(guess)0,5):
 if dic[guess[i]] < tile[i]:
 dic[guess[i]] = tile[i]
 for key in dic:
 color = 47 if dic[key] == -1 else (44 - dic[key])
 print(f'\x1b[1;{color};30m {key.upper()} \x1b[0m', end=' ')
 print(flush=True)
set_of_words = set(words.words()).union(set(open('custom.txt').read().split()))
wordsfive_letter_words = [w for w in set_of_words if (len(w) == WORD_LENGTH]5 and w.isalpha())]
word = (random.choice(wordsfive_letter_words)).lower()
#print(word) # for testing
#word = 'agora' # for testing
guesses = 0
kybd = 'abcdefghijklmnopqrstuvwxyz'
dic=dic = {i: -147 for i in kybd}
guesses = 0
while True: 
 guess = (input('Enter you guess:')).lower()
 while True:
 if guess not in wordsfive_letter_words or len(guess) != WORD_LENGTH5:
 guess = input(f'Not'Not a {WORD_LENGTH}5-letter word!! Guess Again: ')
 else:
 break
 guesses += 1
 if guess == word:
 printTile([2]*WORD_LENGTH[2,2,2,2,2], word)
 print('we have a winner!!!')
 break
 wd_dic = {i: 0 for i in sorted(set(word))}
 for i in wd_dic:
 wd_dic[i] = Occurences(guess,i)
 gu_dic = {i: 0 for i in sorted(set(guess))}
 for i in gu_dic:
 gu_dic[i] = Occurences(word,i)
 tile = [0] * WORD_LENGTH[0,0,0,0,0]
 for i in gu_dic:
 if i in word:
 lst = gu_dic[i].copy()
 if set(wd_dic[i]) & set(lst):
 overlap = list(set(lst).intersection(set(wd_dic[i])))
 for k in overlap:
 tile[k] = 2
 wd_dic[i].remove(k)
 lst.remove(k) 
 for j in lst:
 if j in wd_dic[i] and word[j] != guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 elif word[j]==guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 else:
 if wd_dic[i]:
 tile[wd_dic[i][0]] = 1
 wd_dic[i].pop(0)
 gu_dic[i].remove(j)
 printTile(tile, guess)
 printKeyboard(tile,guess,dic)
 if guesses >== WORD_LENGTH6:
 print(f'You lose!! The word was:')
 printTile([2]*WORD_LENGTH[2,2,2,2,2], word)
 break

Link to custom word file: custom.txt

Link to custom word file of 5-letter words only: custom_5_Letters

Change Log as per "theonlygusti" suggestions:

  • Created custom word files from NLTK dictionaries. So, no need for NLTK dependency. Makes the program run faster.
  • Fixed the error in the keyboard display of words already guessed.
  • Created a global constant for word length.
  • Created two files for differing word lengths. If using 5-letter words only replace:
set_of_words = open('custom.txt').read().split()
words = [w for w in set_of_words if len(w) == WORD_LENGTH]
word = (random.choice(words)).lower()

with

words = list(open('custom_5_letters.txt').read().split())
word = (random.choice(words)).lower()
import time
import random
WORD_LENGTH = 5
def Occurences(s, ch):
 return([i for i, letter in enumerate(s) if letter == ch])
def printTile(tile, guess):
 for i in range(len(guess)):
 color = 44 - tile[i]
 print(f'\x1b[1;{color};30m {guess[i].upper()} \x1b[0m', end=' ')
 time.sleep(0.6)
 print(flush=True)
 
def printKeyboard(tile, guess, dic):
 print()
 for i in range(len(guess)):
 if dic[guess[i]] < tile[i]:
 dic[guess[i]] = tile[i]
 for key in dic:
 color = 47 if dic[key] == -1 else (44 - dic[key])
 print(f'\x1b[1;{color};30m {key.upper()} \x1b[0m', end=' ')
 print(flush=True)
set_of_words = open('custom.txt').read().split()
words = [w for w in set_of_words if len(w) == WORD_LENGTH]
word = (random.choice(words)).lower()
#print(word) # for testing
#word = 'agora' # for testing
kybd = 'abcdefghijklmnopqrstuvwxyz'
dic={i: -1 for i in kybd}
guesses = 0
while True: 
 guess = (input('Enter you guess:')).lower()
 while True:
 if guess not in words or len(guess) != WORD_LENGTH:
 guess = input(f'Not a {WORD_LENGTH}-letter word!! Guess Again: ')
 else:
 break
 guesses += 1
 if guess == word:
 printTile([2]*WORD_LENGTH, word)
 print('we have a winner!!!')
 break
 wd_dic = {i: 0 for i in sorted(set(word))}
 for i in wd_dic:
 wd_dic[i] = Occurences(guess,i)
 gu_dic = {i: 0 for i in sorted(set(guess))}
 for i in gu_dic:
 gu_dic[i] = Occurences(word,i)
 tile = [0] * WORD_LENGTH
 for i in gu_dic:
 if i in word:
 lst = gu_dic[i].copy()
 if set(wd_dic[i]) & set(lst):
 overlap = list(set(lst).intersection(set(wd_dic[i])))
 for k in overlap:
 tile[k] = 2
 wd_dic[i].remove(k)
 lst.remove(k) 
 for j in lst:
 if j in wd_dic[i] and word[j] != guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 elif word[j]==guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 else:
 if wd_dic[i]:
 tile[wd_dic[i][0]] = 1
 wd_dic[i].pop(0)
 gu_dic[i].remove(j)
 printTile(tile, guess)
 printKeyboard(tile,guess,dic)
 if guesses > WORD_LENGTH:
 print(f'You lose!! The word was:')
 printTile([2]*WORD_LENGTH, word)
 break

NLTK needs to be installed to use the code.

Link to custom word file to supplement words in NLTK dictionary: custom.txt

import time
from nltk.corpus import words
from nltk.corpus import wordnet 
import random
def Occurences(s, ch):
 return([i for i, letter in enumerate(s) if letter == ch])
def printTile(tile, guess):
 for i in range(0,5):
 color = 44 - tile[i]
 print(f'\x1b[1;{color};30m {guess[i].upper()} \x1b[0m', end=' ')
 time.sleep(0.6)
 print(flush=True)
 
def printKeyboard(tile, guess, dic):
 print()
 for key in dic:
 if dic[key] != 0:
 dic[key] = -3
 for i in range(0,5):
 if dic[guess[i]] < tile[i]:
 dic[guess[i]] = tile[i]
 for key in dic:
 color = 44 - dic[key]
 print(f'\x1b[1;{color};30m {key.upper()} \x1b[0m', end=' ')
 print(flush=True)
set_of_words = set(words.words()).union(set(open('custom.txt').read().split()))
five_letter_words = [w for w in set_of_words if (len(w) == 5 and w.isalpha())]
word = (random.choice(five_letter_words)).lower()
#print(word) # for testing
#word = 'agora' # for testing
guesses = 0
kybd = 'abcdefghijklmnopqrstuvwxyz'
dic = {i: 47 for i in kybd}
while True: 
 guess = (input('Enter you guess:')).lower()
 while True:
 if guess not in five_letter_words or len(guess) != 5:
 guess = input('Not a 5-letter word!! Guess Again: ')
 else:
 break
 guesses += 1
 if guess == word:
 printTile([2,2,2,2,2], word)
 print('we have a winner!!!')
 break
 wd_dic = {i: 0 for i in sorted(set(word))}
 for i in wd_dic:
 wd_dic[i] = Occurences(guess,i)
 gu_dic = {i: 0 for i in sorted(set(guess))}
 for i in gu_dic:
 gu_dic[i] = Occurences(word,i)
 tile = [0,0,0,0,0]
 for i in gu_dic:
 if i in word:
 lst = gu_dic[i].copy()
 if set(wd_dic[i]) & set(lst):
 overlap = list(set(lst).intersection(set(wd_dic[i])))
 for k in overlap:
 tile[k] = 2
 wd_dic[i].remove(k)
 lst.remove(k) 
 for j in lst:
 if j in wd_dic[i] and word[j] != guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 elif word[j]==guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 else:
 if wd_dic[i]:
 tile[wd_dic[i][0]] = 1
 wd_dic[i].pop(0)
 gu_dic[i].remove(j)
 printTile(tile, guess)
 printKeyboard(tile,guess,dic)
 if guesses == 6:
 print(f'You lose!! The word was:')
 printTile([2,2,2,2,2], word)
 break
added 468 characters in body
Source Link

NLTK needs to be installed to use the code.

Link to custom word file to supplement words in NLTK dictionary: custom.txt

Link to custom word file of 5-letter words only: custom_5_Letters

Change Log as per "theonlygusti" suggestions:

  • Created custom word files from NLTK dictionaries. So, no need for NLTK dependency. Makes the program run faster.
  • Fixed the error in the keyboard display of words already guessed.
  • Created a global constant for word length.
  • Created two files for differing word lengths. If using 5-letter words only replace:
importset_of_words time
from= nltkopen('custom.corpustxt').read().split()
words import= [w for w in set_of_words if len(w) == WORD_LENGTH]
word = (random.choice(words)).lower()
from

with

words nltk= list(open('custom_5_letters.corpustxt').read().split())
word import= wordnet(random.choice(words)).lower()
import time
import random
WORD_LENGTH = 5
def Occurences(s, ch):
 return([i for i, letter in enumerate(s) if letter == ch])
def printTile(tile, guess):
 for i in range(0,5len(guess)):
 color = 44 - tile[i]
 print(f'\x1b[1;{color};30m {guess[i].upper()} \x1b[0m', end=' ')
 time.sleep(0.6)
 print(flush=True)
 
def printKeyboard(tile, guess, dic):
 print()
 for key in dic:
 if dic[key] != 0:
 dic[key] = -3
 for i in range(0,5len(guess)):
 if dic[guess[i]] < tile[i]:
 dic[guess[i]] = tile[i]
 for key in dic:
 color = 47 if dic[key] == -1 else (44 - dic[key])
 print(f'\x1b[1;{color};30m {key.upper()} \x1b[0m', end=' ')
 print(flush=True)
set_of_words = set(words.words()).union(set(open('custom.txt').read().split()))
five_letter_wordswords = [w for w in set_of_words if (len(w) == 5 and w.isalpha())]WORD_LENGTH]
word = (random.choice(five_letter_wordswords)).lower()
#print(word) # for testing
#word = 'agora' # for testing
guesses = 0
kybd = 'abcdefghijklmnopqrstuvwxyz'
dic = dic={i: 47-1 for i in kybd}
guesses = 0
while True: 
 guess = (input('Enter you guess:')).lower()
 while True:
 if guess not in five_letter_wordswords or len(guess) != 5WORD_LENGTH:
 guess = input('Notf'Not a 5{WORD_LENGTH}-letter word!! Guess Again: ')
 else:
 break
 guesses += 1
 if guess == word:
 printTile([2,2,2,2,2][2]*WORD_LENGTH, word)
 print('we have a winner!!!')
 break
 wd_dic = {i: 0 for i in sorted(set(word))}
 for i in wd_dic:
 wd_dic[i] = Occurences(guess,i)
 gu_dic = {i: 0 for i in sorted(set(guess))}
 for i in gu_dic:
 gu_dic[i] = Occurences(word,i)
 tile = [0,0,0,0,0][0] * WORD_LENGTH
 for i in gu_dic:
 if i in word:
 lst = gu_dic[i].copy()
 if set(wd_dic[i]) & set(lst):
 overlap = list(set(lst).intersection(set(wd_dic[i])))
 for k in overlap:
 tile[k] = 2
 wd_dic[i].remove(k)
 lst.remove(k) 
 for j in lst:
 if j in wd_dic[i] and word[j] != guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 elif word[j]==guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 else:
 if wd_dic[i]:
 tile[wd_dic[i][0]] = 1
 wd_dic[i].pop(0)
 gu_dic[i].remove(j)
 printTile(tile, guess)
 printKeyboard(tile,guess,dic)
 if guesses ==> 6WORD_LENGTH:
 print(f'You lose!! The word was:')
 printTile([2,2,2,2,2][2]*WORD_LENGTH, word)
 break

NLTK needs to be installed to use the code.

Link to custom word file to supplement words in NLTK dictionary: custom.txt

import time
from nltk.corpus import words
from nltk.corpus import wordnet 
import random
def Occurences(s, ch):
 return([i for i, letter in enumerate(s) if letter == ch])
def printTile(tile, guess):
 for i in range(0,5):
 color = 44 - tile[i]
 print(f'\x1b[1;{color};30m {guess[i].upper()} \x1b[0m', end=' ')
 time.sleep(0.6)
 print(flush=True)
 
def printKeyboard(tile, guess, dic):
 print()
 for key in dic:
 if dic[key] != 0:
 dic[key] = -3
 for i in range(0,5):
 if dic[guess[i]] < tile[i]:
 dic[guess[i]] = tile[i]
 for key in dic:
 color = 44 - dic[key]
 print(f'\x1b[1;{color};30m {key.upper()} \x1b[0m', end=' ')
 print(flush=True)
set_of_words = set(words.words()).union(set(open('custom.txt').read().split()))
five_letter_words = [w for w in set_of_words if (len(w) == 5 and w.isalpha())]
word = (random.choice(five_letter_words)).lower()
#print(word) # for testing
#word = 'agora' # for testing
guesses = 0
kybd = 'abcdefghijklmnopqrstuvwxyz'
dic = {i: 47 for i in kybd}
while True: 
 guess = (input('Enter you guess:')).lower()
 while True:
 if guess not in five_letter_words or len(guess) != 5:
 guess = input('Not a 5-letter word!! Guess Again: ')
 else:
 break
 guesses += 1
 if guess == word:
 printTile([2,2,2,2,2], word)
 print('we have a winner!!!')
 break
 wd_dic = {i: 0 for i in sorted(set(word))}
 for i in wd_dic:
 wd_dic[i] = Occurences(guess,i)
 gu_dic = {i: 0 for i in sorted(set(guess))}
 for i in gu_dic:
 gu_dic[i] = Occurences(word,i)
 tile = [0,0,0,0,0]
 for i in gu_dic:
 if i in word:
 lst = gu_dic[i].copy()
 if set(wd_dic[i]) & set(lst):
 overlap = list(set(lst).intersection(set(wd_dic[i])))
 for k in overlap:
 tile[k] = 2
 wd_dic[i].remove(k)
 lst.remove(k) 
 for j in lst:
 if j in wd_dic[i] and word[j] != guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 elif word[j]==guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 else:
 if wd_dic[i]:
 tile[wd_dic[i][0]] = 1
 wd_dic[i].pop(0)
 gu_dic[i].remove(j)
 printTile(tile, guess)
 printKeyboard(tile,guess,dic)
 if guesses == 6:
 print(f'You lose!! The word was:')
 printTile([2,2,2,2,2], word)
 break

Link to custom word file: custom.txt

Link to custom word file of 5-letter words only: custom_5_Letters

Change Log as per "theonlygusti" suggestions:

  • Created custom word files from NLTK dictionaries. So, no need for NLTK dependency. Makes the program run faster.
  • Fixed the error in the keyboard display of words already guessed.
  • Created a global constant for word length.
  • Created two files for differing word lengths. If using 5-letter words only replace:
set_of_words = open('custom.txt').read().split()
words = [w for w in set_of_words if len(w) == WORD_LENGTH]
word = (random.choice(words)).lower()

with

words = list(open('custom_5_letters.txt').read().split())
word = (random.choice(words)).lower()
import time
import random
WORD_LENGTH = 5
def Occurences(s, ch):
 return([i for i, letter in enumerate(s) if letter == ch])
def printTile(tile, guess):
 for i in range(len(guess)):
 color = 44 - tile[i]
 print(f'\x1b[1;{color};30m {guess[i].upper()} \x1b[0m', end=' ')
 time.sleep(0.6)
 print(flush=True)
 
def printKeyboard(tile, guess, dic):
 print()
 for i in range(len(guess)):
 if dic[guess[i]] < tile[i]:
 dic[guess[i]] = tile[i]
 for key in dic:
 color = 47 if dic[key] == -1 else (44 - dic[key])
 print(f'\x1b[1;{color};30m {key.upper()} \x1b[0m', end=' ')
 print(flush=True)
set_of_words = open('custom.txt').read().split()
words = [w for w in set_of_words if len(w) == WORD_LENGTH]
word = (random.choice(words)).lower()
#print(word) # for testing
#word = 'agora' # for testing
kybd = 'abcdefghijklmnopqrstuvwxyz'
dic={i: -1 for i in kybd}
guesses = 0
while True: 
 guess = (input('Enter you guess:')).lower()
 while True:
 if guess not in words or len(guess) != WORD_LENGTH:
 guess = input(f'Not a {WORD_LENGTH}-letter word!! Guess Again: ')
 else:
 break
 guesses += 1
 if guess == word:
 printTile([2]*WORD_LENGTH, word)
 print('we have a winner!!!')
 break
 wd_dic = {i: 0 for i in sorted(set(word))}
 for i in wd_dic:
 wd_dic[i] = Occurences(guess,i)
 gu_dic = {i: 0 for i in sorted(set(guess))}
 for i in gu_dic:
 gu_dic[i] = Occurences(word,i)
 tile = [0] * WORD_LENGTH
 for i in gu_dic:
 if i in word:
 lst = gu_dic[i].copy()
 if set(wd_dic[i]) & set(lst):
 overlap = list(set(lst).intersection(set(wd_dic[i])))
 for k in overlap:
 tile[k] = 2
 wd_dic[i].remove(k)
 lst.remove(k) 
 for j in lst:
 if j in wd_dic[i] and word[j] != guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 elif word[j]==guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 else:
 if wd_dic[i]:
 tile[wd_dic[i][0]] = 1
 wd_dic[i].pop(0)
 gu_dic[i].remove(j)
 printTile(tile, guess)
 printKeyboard(tile,guess,dic)
 if guesses > WORD_LENGTH:
 print(f'You lose!! The word was:')
 printTile([2]*WORD_LENGTH, word)
 break
Rollback to Revision 4
Source Link
pacmaninbw
  • 26.1k
  • 13
  • 47
  • 114
import time
from nltk.corpus import words
from nltk.corpus import wordnet 
import random
def Occurences(s, ch):
 return([i for i, letter in enumerate(s) if letter == ch])
def printTile(tile, guess):
 for i in range(0,5):
 color = 44 - tile[i]
 print(f'\x1b[1;{color};30m {guess[i].upper()} \x1b[0m', end=' ')
 time.sleep(0.6)
 print(flush=True)
 
def printKeyboard(tile, guess, dic):
 print()
 for key in dic:
 if dic[key] != 0:
 dic[key] = -3
 for i in range(0,5):
 if dic[guess[i]] < tile[i]:
 dic[guess[i]] = tile[i]
 for key in dic:
 color = 47 if dic[key] == -1 else (44 - dic[key])
 print(f'\x1b[1;{color};30m {key.upper()} \x1b[0m', end=' ')
 print(flush=True)
set_of_words = set(words.words()).union(set(wordnet.words())).union(set(open('custom.txt').read().split()))
five_letter_words = [w for w in set_of_words if (len(w) == 5 and w.isalpha())]
word = (random.choice(five_letter_words)).lower()
#print(word) # for testing
#word = 'baker''agora' # for testing
guesses = 0
kybd = 'abcdefghijklmnopqrstuvwxyz'
dic=dic = {i: -147 for i in kybd}
guesses = 0
while True: 
 guess = (input('Enter you guess:')).lower()
 while True:
 if guess not in five_letter_words or len(guess) != 5:
 guess = input('Not a 5-letter word!! Guess Again: ')
 else:
 break
 guesses += 1
 if guess == word:
 printTile([2,2,2,2,2], word)
 print('we have a winner!!!')
 break
 wd_dic = {i: 0 for i in sorted(set(word))}
 for i in wd_dic:
 wd_dic[i] = Occurences(guess,i)
 gu_dic = {i: 0 for i in sorted(set(guess))}
 for i in gu_dic:
 gu_dic[i] = Occurences(word,i)
 tile = [0,0,0,0,0]
 for i in gu_dic:
 if i in word:
 lst = gu_dic[i].copy()
 if set(wd_dic[i]) & set(lst):
 overlap = list(set(lst).intersection(set(wd_dic[i])))
 for k in overlap:
 tile[k] = 2
 wd_dic[i].remove(k)
 lst.remove(k) 
 for j in lst:
 if j in wd_dic[i] and word[j] != guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 elif word[j]==guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 else:
 if wd_dic[i]:
 tile[wd_dic[i][0]] = 1
 wd_dic[i].pop(0)
 gu_dic[i].remove(j)
 printTile(tile, guess)
 printKeyboard(tile,guess,dic)
 if guesses == 6:
 print(f'You lose!! The word was:')
 printTile([2,2,2,2,2], word)
 break
import time
from nltk.corpus import words
from nltk.corpus import wordnet 
import random
def Occurences(s, ch):
 return([i for i, letter in enumerate(s) if letter == ch])
def printTile(tile, guess):
 for i in range(0,5):
 color = 44 - tile[i]
 print(f'\x1b[1;{color};30m {guess[i].upper()} \x1b[0m', end=' ')
 time.sleep(0.6)
 print(flush=True)
 
def printKeyboard(tile, guess, dic):
 print()
 for i in range(0,5):
 if dic[guess[i]] < tile[i]:
 dic[guess[i]] = tile[i]
 for key in dic:
 color = 47 if dic[key] == -1 else (44 - dic[key])
 print(f'\x1b[1;{color};30m {key.upper()} \x1b[0m', end=' ')
 print(flush=True)
set_of_words = set(words.words()).union(set(wordnet.words())).union(set(open('custom.txt').read().split()))
five_letter_words = [w for w in set_of_words if (len(w) == 5 and w.isalpha())]
word = (random.choice(five_letter_words)).lower()
#print(word) # for testing
#word = 'baker' # for testing
kybd = 'abcdefghijklmnopqrstuvwxyz'
dic={i: -1 for i in kybd}
guesses = 0
while True: 
 guess = (input('Enter you guess:')).lower()
 while True:
 if guess not in five_letter_words or len(guess) != 5:
 guess = input('Not a 5-letter word!! Guess Again: ')
 else:
 break
 guesses += 1
 if guess == word:
 printTile([2,2,2,2,2], word)
 print('we have a winner!!!')
 break
 wd_dic = {i: 0 for i in sorted(set(word))}
 for i in wd_dic:
 wd_dic[i] = Occurences(guess,i)
 gu_dic = {i: 0 for i in sorted(set(guess))}
 for i in gu_dic:
 gu_dic[i] = Occurences(word,i)
 tile = [0,0,0,0,0]
 for i in gu_dic:
 if i in word:
 lst = gu_dic[i].copy()
 if set(wd_dic[i]) & set(lst):
 overlap = list(set(lst).intersection(set(wd_dic[i])))
 for k in overlap:
 tile[k] = 2
 wd_dic[i].remove(k)
 lst.remove(k) 
 for j in lst:
 if j in wd_dic[i] and word[j] != guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 elif word[j]==guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 else:
 if wd_dic[i]:
 tile[wd_dic[i][0]] = 1
 wd_dic[i].pop(0)
 gu_dic[i].remove(j)
 printTile(tile, guess)
 printKeyboard(tile,guess,dic)
 if guesses == 6:
 print(f'You lose!! The word was:')
 printTile([2,2,2,2,2], word)
 break
import time
from nltk.corpus import words
from nltk.corpus import wordnet 
import random
def Occurences(s, ch):
 return([i for i, letter in enumerate(s) if letter == ch])
def printTile(tile, guess):
 for i in range(0,5):
 color = 44 - tile[i]
 print(f'\x1b[1;{color};30m {guess[i].upper()} \x1b[0m', end=' ')
 time.sleep(0.6)
 print(flush=True)
 
def printKeyboard(tile, guess, dic):
 print()
 for key in dic:
 if dic[key] != 0:
 dic[key] = -3
 for i in range(0,5):
 if dic[guess[i]] < tile[i]:
 dic[guess[i]] = tile[i]
 for key in dic:
 color = 44 - dic[key]
 print(f'\x1b[1;{color};30m {key.upper()} \x1b[0m', end=' ')
 print(flush=True)
set_of_words = set(words.words()).union(set(open('custom.txt').read().split()))
five_letter_words = [w for w in set_of_words if (len(w) == 5 and w.isalpha())]
word = (random.choice(five_letter_words)).lower()
#print(word) # for testing
#word = 'agora' # for testing
guesses = 0
kybd = 'abcdefghijklmnopqrstuvwxyz'
dic = {i: 47 for i in kybd}
while True: 
 guess = (input('Enter you guess:')).lower()
 while True:
 if guess not in five_letter_words or len(guess) != 5:
 guess = input('Not a 5-letter word!! Guess Again: ')
 else:
 break
 guesses += 1
 if guess == word:
 printTile([2,2,2,2,2], word)
 print('we have a winner!!!')
 break
 wd_dic = {i: 0 for i in sorted(set(word))}
 for i in wd_dic:
 wd_dic[i] = Occurences(guess,i)
 gu_dic = {i: 0 for i in sorted(set(guess))}
 for i in gu_dic:
 gu_dic[i] = Occurences(word,i)
 tile = [0,0,0,0,0]
 for i in gu_dic:
 if i in word:
 lst = gu_dic[i].copy()
 if set(wd_dic[i]) & set(lst):
 overlap = list(set(lst).intersection(set(wd_dic[i])))
 for k in overlap:
 tile[k] = 2
 wd_dic[i].remove(k)
 lst.remove(k) 
 for j in lst:
 if j in wd_dic[i] and word[j] != guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 elif word[j]==guess[j]:
 tile[j] = 2
 wd_dic[i].remove(j)
 else:
 if wd_dic[i]:
 tile[wd_dic[i][0]] = 1
 wd_dic[i].pop(0)
 gu_dic[i].remove(j)
 printTile(tile, guess)
 printKeyboard(tile,guess,dic)
 if guesses == 6:
 print(f'You lose!! The word was:')
 printTile([2,2,2,2,2], word)
 break
deleted 19 characters in body
Source Link
Loading
added 52 characters in body
Source Link
Loading
deleted 30 characters in body
Source Link
Loading
added 425 characters in body
Source Link
Loading
Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /