stack overflow
stack overflow

Reputation: 29

How To create an anagram of a word?

how can i make an anagram of a word using random.shuffle? so far I have this this comes up with a error message:any help is very much appreciated:

    import random
    word = "house"
    random.shuffle(word)
    print(word)

Upvotes: 1

Views: 1994

Answers (1)

W45HT0N
W45HT0N

Reputation: 36

The function shuffle in the random module shuffles in-place, so you first have to convert your string to a list of characters, shuffle it, then join the result again.

import random
word = "house"
l = list(word)
random.shuffle(l)
result = ''.join(l)
print(result)

Upvotes: 2

Related Questions