Python: How to print my simple poem
Question: I would like to know how I could print out sentences using my triplet poem
program. My program randomly picks a list of nouns to use.

My program:

    
    
    import random
    
    def nouns():
        nounPersons = ["cow","crowd","hound","clown"];
        nounPlace = ["town","pound","battleground","playground"];
        rhymes = ["crowned","round","gowned","found","drowned"];
    
        nounPersons2 = ["dog","frog","hog"];
        nounPlace2 = ["fog","Prague","smog"];
        rhymes2 = ["log","eggnog","hotdog"];
    
        nounList1 = [nounPersons,nounPlace,rhymes]
        nounList2 = [nounPersons2,nounPlace2,rhymes2]
        nounsList = [nounList1, nounList2]
        randomPick = random.choice(nounsList)
        return(randomPick)
    
    verbs = ["walked","ran","rolled","biked","crawled"];
    nouns()
    

For example, I could have "The cow walked to the town. But then it was
drowned." And just replace the nouns/rhyme(cow, town,drowned) and verb(walked)
with my randomizer.

Would I use random.randint in some way?

I just basically need a general print statement like the example I showed
using my randomizer to randomly pick between the nouns/rhymes.


Answer: As usual (for me), there may be a more Pythonic approach, but to get what you
have working, I did three things:

  1. assigned your call to the nouns() function to 'chosen_list' variable. That way the returned 'randomPick' gets used.

  2. built in a selection step to get individual words from the lists in 'chosen_list' and your verb list

  3. added a final print statement with formatting to assemble the words in to a sentence

the code:

    
    
    import random
    def nouns():
    
        nounPersons = ["cow","crowd","hound","clown"];
        nounPlace = ["town","pound","battleground","playground"];
        rhymes = ["crowned","round","gowned","found","drowned"];
    
        nounPersons2 = ["dog","frog","hog"];
        nounPlace2 = ["fog","Prague","smog"];
        rhymes2 = ["log","eggnog","hotdog"];
    
        nounList1 = [nounPersons,nounPlace,rhymes]
        nounList2 = [nounPersons2,nounPlace2,rhymes2]
        nounsList = [nounList1, nounList2]
        randomPick = random.choice(nounsList)
    
        return randomPick
    
    verbs = ["walked","ran","rolled","biked","crawled"]
    
    # this is change 1.
    chosen_list = nouns()
    
    # select single words from lists - this is change 2.
    
    noun_subj = random.choice(chosen_list[0])
    noun_obj = random.choice(chosen_list[1])
    rhyme_word = random.choice(chosen_list[2])
    verb_word = random.choice(verbs)
    
    # insert words in to text line - this is change 3.
    
    print ("The {} {} to the {}. But then it was {}.".format(noun_subj, verb_word, noun_obj, rhyme_word))
    

