Brand Mellor Brand Mellor - 6 months ago 22
Python Question

Insult Generator Gone Wrong

I am making an insult generator for class that uses one word from three lists within a tuple named

allWords
. For some reason, while my
generateInsult()
function generates an insult, my
generateInsults()
function generates
"Thou A l l!"


Does anybody have any idea what is going on?

Here's the relevant code

def generateInsult(word='allWords'):
insult = "Thou " + random.choice(word[0]) + " " + random.choice(word[1]) + " " + random.choice(word[2]) + "!"
return insult

def generateInsults(word='allWords'):
insults = list()
for i in range(100):
insults.append(generateInsult('AllWords'))
return insults

Answer

I understand that you have a 3-tuple of lists called allWords. You should remove the quotes around the default argument, ie change the following lines:

def generateInsult(word=allWords):
    ...

def generateInsults(word=allWords):
    ...
    insults.append(generateInsult(word))