Suppose I'm creating a madLib and i want to replace each word from a string that has the word 'plural noun'. Basically, the user gets a prompt that states to input plural nouns and those inputs the go to a dictionary (pluralnoDict).
I've been using random.choice, and it's been working out, however, the repeats is obviously the issue. I tried random.sample, however, instead of choosing one word from a given sample, the code replaces the words with the whole sample.
There a way I can replace each string using random.sample from a dictionary list? For example:
Original: The 'plural noun' have 'plural noun' and 'plural noun'. Expected: The 'birds' have 'wings' and 'feet'.
Below is the for loop i use to replace the plural noun strings.
for key in pluralnoDict:
target_word = "({0})".format(key)
while target_word in madString:
madString = madString.replace(target_word,random.choice(pluralnoDict[key]), 1)
Aucun commentaire:
Enregistrer un commentaire