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:
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. | Example 2 |
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.
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
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
.
- Add at least 3 more noun options that will be randomly selected for this line.
- Test your function and make sure it works (run
firstLine()
in the shell to see the result directly, or putprint(firstLine())
at the bottom of your file to have it print one result every time you run the file). - (Optional) Change the base line as well to better reflect you and your partner today.
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.
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
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:
- Choose a three-syllable base expression. We'll use "
then you are
" as our example. - Use
random.choice
to choose from a set of 2-syllable verbs. In our example, we use['eating', 'singing', 'dancing']
. - 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
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.