Lab 3, Poems

In this part, you'll write fruitful functions that return strings.

Two things that are useful in this task:

1. Newline character \n

In Python, to get a new line in a string, use \n. Here are two examples shown in Thonny's shell:

  1. Thonny shell showing the string "hello-backslash-n-world" and also showing results of printing the same string. When typed into the shell as a value, the result looks the same as the input, including the backslash-n pair. When printed, "hello" appears on one line and "world" appears on a second line.
  2. Thonny shell showing the string "red-backslash-n-green-backslash-n-blue" and also showing results of printing the same string. As before, it appears on one line as a value but when printed the words "red", "green", and "blue" each appear on a separate line.

2. Python's random.choice function

random.choice is a fruitful function that returns a randomly chosen item from the given options. Here are some examples, again shown in Thonny's shell.

Example 1. Thonny shell showing invocations of random.choice with three numbers: 3, 5, and 7. The numbers are separated by commas and placed within square brackets inside the parentheses for the function call. The results are random even for repeated calls to the same function with the same argument: first 5, then 3, then 7, and then 7 again. Example 2 Thonny shell showing invocations of random.choice with three strings: bagel, muffin and donut. Again the stirngs are placed inside square brackets inside the function call parentheses, and separated with commas. The results are again different each time: "donut", then "muffin", then "bagel", then "muffin" again.

Note that random.choice requires an import statement at the top of your file:

import random

and also, random.choice requires that the options to choose from are tucked inside square brackets (this is called a list, which we will cover later this semester). This image points out the square brackets in the random.choice invocations.

Thonny shell showing 2 invocations of random.choice with the square brackets marked with large green arrows. Here is the first example as text: random.choice([3, 5, 7]). The second example: random.choice(["donut", "bagel", "muffin"])

Poetry writing

Now you're going to write some Python code to generate some haikus (haikus are 3 line poems with 5 syllables, then 7 syllables, then 5 syllables. Parts of your poem will be randomly generated. Here is a sample haiku:

computer science       # 5 syllables 
python is so alluring  # 7 syllables
dang indentation       # 5 syllables

Task 1. Modify firstLine

Partner B

Open lab03/poems.py and find the firstLine function. Note that it is fruitful (meaning it returns a value) and that is uses random.choice.

With just two noun choices (me and them), calling the poem function returns this poem (note the newline characters tucked in there):

'you say you like me\nyou say you like me\nyou say you like them\n'

If you print the value that poem returns, you might see this (your results will vary due to the randomness involved):

>>> print(poem())
you say you like me
you say you like me
you say you like them

Task 2. Write thirdLine

We're going to write the third line function before the second line function because the first line and third line both have 5 syllables. We'll do the middle 7-syllable line last in Task 3 below.

Partner A

TASK 2A. Write a function called thirdLine that will return a string with 5 syllables. For this function, write at least 3 different 5 syllable lines. Save them in variables called line1, line2 and line3. For example, you might have:

     line1 = 'I gave you my heart'

Then, in your thirdLine function, use random.choice to choose one of the three lines like this:

     random.choice([line1, line2, line3])

Make sure that your thirdLine function returns a five-syllable string.

TASK 2B. Update your poem function to call thirdLine by replacing the last call to firstLine with a call to thirdLine.

Your poems might look something like this (again, yours will be different due to the randomness involved):

    >>> print(poem())

      you say you like them
      you say you like me
      classic betrayal
    >>> print(poem())

      you say you like me
      you say you like me
      under the covers

Task 3. Write secondLine

Partner B

TASK 3A. Write a function called secondLine that will return a string with 7 syllables. This is for the middle line of your haikus. Here is how to generate your 7 syllable string:

  1. Choose a three-syllable base expression. We'll use "then you are" as our example.
  2. Use random.choice to choose from a set of 2-syllable verbs. In our example, we use ['eating', 'singing', 'dancing'].
  3. Use random.choice to choose from a set of 2-syllable descriptive phrases. In our example, we use ['with cats', 'outside', 'alone'].

TASK 3B. Update your poem function to call secondLine by replacing the middle call to firstLine with a call to secondLine. Your poems might look something like this (again, yours will be different due to the randomness involved):

    >>> print(poem())

     you say you like them
     then you are dancing with cats
     i gave you my heart
    >>> print(poem())

     you say you like me
     then you are eating alone
     under the covers

Now you have your own haiku generator!

Task 4. Write haikuFlurry

Partner A

Write a fruitful function called haikuFlurry that will return a string containing 3 haikus. Below is one example (yours will be different; you know why).

    >>> print(haikuFlurry())

     you say you like them
     then you are dancing outside
     believe you? never!

     you say you like snow
     then you are singing with cats
     classic betrayal

     you say you like rice
     then you are eating alone
     i gave you my heart

Task 5. Submit your best poem(s) to add to our CS111 catalog.

Fill out this form and share your poem with us (you can submit it anonymously or include your names).

Click here to see poems from cs111 students.

Table of Contents