#anagrams.py #using the words_merged.txt list import random words = open("words_merged.txt").read().split() words.sort(key=lambda s: len(s)) print("#words:",len(words)) word_length = int(input("Length of anagrams you want (1-22): ")) if word_length<1 or word_length>22: # antidisestablishmentarianism is out print("Invalid length. Quitting") exit() #start and stop index of words of this length: start = 0 while len(words[start]) < word_length: start += 1 stop = start while len(words[stop]) == word_length: stop += 1 #print(words[start],words[stop-1], " #words this length:",stop-start) print(" #words this length:",stop-start) another = True while another: chosen_word = words[random.randint(start,stop-1)] derangement = False while not derangement: l = list(chosen_word) random.shuffle(l) anagram = ''.join(l) if anagram != chosen_word: derangement = True print("\n",anagram) show = input("Show the word? (y or n) ") if show!='n': print(chosen_word)