0

I am trying to create an anagram maker. However, when I run this code the while loop goes on forever, and "combinations" doesn't seem to be getting items added to it. Why is this the case?

This question was marked as a duplicate and I'm not sure why. This is the first time I've asked it. I'd appreciate if you'd answer it.

x = input("Give us a word and I will supply all possible anagrams: ")

list = []
combinations = [x]

for letter in x:
    list.append(letter)

length = int(len(list))
while len(combinations) < math.factorial(length):
    y = random.shuffle(list)
    if y not in combinations:
        combinations.append(y)

print(combinations)
  • is there a letter present twice in your input ? – njzk2 Apr 30 '17 at 19:48
  • also, you may want to read the documentation for [random.shuffle](https://docs.python.org/2/library/random.html#random.shuffle) – njzk2 Apr 30 '17 at 19:49
  • @njzk2 how is that relevant? –  Apr 30 '17 at 19:49
  • @njzk2 thanks, will do –  Apr 30 '17 at 19:50
  • @njzk2 done. didn't seem to get me anywhere tho :/ –  Apr 30 '17 at 19:50
  • `random.shuffle` modifies its input. If you do `y = random.shuffle(list)`, y will be `None`. – ayhan Apr 30 '17 at 19:52
  • @ayhan what could i use instead of shuffle? –  Apr 30 '17 at 20:01
  • see http://stackoverflow.com/questions/17649875/why-does-random-shuffle-return-none – njzk2 Apr 30 '17 at 20:07
  • 1
    As for duplicate letters, it means that for input `aa`, there is only one permutation, which is `aa`, despite `!2 == 2`, so you won't be able to get out of the loop with that condition – njzk2 Apr 30 '17 at 20:08
  • use random.shuffle(list, len(list)) instead of shuffle and it would work*. Also read the documentation on random.shuffle. Someone has put the link on the comments * - if there aren't any duplicates – RPT Apr 30 '17 at 20:08
  • You don't need to use something else. You can just change the list or take a copy. There are alternatives in the duplicate question. – ayhan Apr 30 '17 at 20:09

0 Answers0