@extends('template')
@section('title')
Lab 3, Poems
@stop
@section('content')
# 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.
2.
### 2. Python's `random.choice` function
[`random.choice`](https://www.w3schools.com/python/ref_random_choice.asp) 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:
```py
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:
```py
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`.
+ 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 put `print(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):
```xml
'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):
```xml
>>> 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:
```py
line1 = 'I gave you my heart'
```
Then, in your `thirdLine` function, use `random.choice` to choose one of the three lines like this:
```py
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`.
{{--
__Python tip__:Look at the `poem` function. Do you see those slashes in the **return** statement? What are those for?
Click here to show the answer
A slash like this `\` tells Thonny that you are continuing this line of code on the next line. This helps you avoid very long lines of code and also makes it
easier to understand your code when organized like this. Without the `\`, the code would generate an error. In this case, we are returning a string that is composed of many smaller strings concatenated (+) together.
--}}
Your poems might look something like this (again, yours will be different due to the randomness involved):
```xml
>>> print(poem())
you say you like them
you say you like me
classic betrayal
```
```xml
>>> 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):
```xml
>>> print(poem())
you say you like them
then you are dancing with cats
i gave you my heart
```
```xml
>>> 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).
```xml
>>> 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.
@include('/labs/lab03/_toc')
@stop