import random from lib import cat_words, INSTRUCTIONS, BLANK, ROUNDS, WORD_LENGTH PLAY_INSTRUCTIONS = "Guess a 5-letter word, all lowercase. Hints will be given after the guess. " + INSTRUCTIONS def build_letter_hint(guess, word): guess_break = [letter for letter in guess] word_break = [letter for letter in word] letters_hints = [BLANK] * WORD_LENGTH for guess_pos, guess_letter in enumerate(guess): if word[guess_pos] == guess_letter: letters_hints[guess_pos] = guess_letter.upper() word_break[guess_pos] = BLANK guess_break[guess_pos] = BLANK # see test() - need to be more careful with repeating words for guess_pos, guess_letter in enumerate(guess_break): if guess_letter != BLANK and guess_letter in word_break: letters_hints[guess_pos] = guess_letter first_index = word_break.index(guess_letter) word_break[first_index] = BLANK return ''.join(letters_hints) def build_non_letter_hint(guess, word): non_letters_hints = '' for letter in guess: if letter not in word and letter not in non_letters_hints: non_letters_hints += letter return non_letters_hints def main_loop(): word = random.choice([w for w in cat_words()]) print(PLAY_INSTRUCTIONS) for round_number in range(ROUNDS): print("ROUND {}".format(round_number + 1)) guess = input("Guess: ") if len(guess) != 5 and not all([x.islower() for x in guess]): print("ERROR: {}".format(PLAY_INSTRUCTIONS)) non_letters_hints = build_non_letter_hint(guess, word) letters_hints = build_letter_hint(guess, word) print("Non-letters: {}".format(non_letters_hints)) print("Letters: {}".format(letters_hints)) print("-" * 10) if all([x.isupper() for x in letters_hints]): print('WIN') break else: print("LOSE") print("Word was: {}".format(word)) def do_test(): assert build_letter_hint('yummy', 'slily') == '----Y' assert build_letter_hint('yummy', 'slyly') == 'y---Y' assert build_letter_hint('yymyy', 'slyyy') == 'y--YY' assert build_letter_hint('ymmyy', 'slyyy') == 'y--YY' assert build_letter_hint('mmmyy', 'slyyy') == '---YY' assert build_letter_hint('cabby', 'abbey') == '-aBbY' assert build_letter_hint('abbey', 'cabby') == 'abB-Y' def main(): do_test() main_loop() if __name__ == "__main__": main()