Introduction
In this project, you will learn how to decipher the hidden message in an acrostic poem. Acrostic poems are a type of poetry where the first letters of each line spell out a word or phrase. Your task is to write a Python function that can extract the first words of each line and concatenate them to reveal the hidden message.
👀 Preview
## Example
$ python3 acrostic_poetry.py
RainItItAnd
🎯 Tasks
In this project, you will learn:
- How to understand the problem statement and requirements for the Acrostic Poetry Decryption project
- How to implement the
acrostic_poetry_decryption(poem)function to decrypt the message in the acrostic poem - How to test the decryption function with an example input
🏆 Achievements
After completing this project, you will be able to:
- Understand and solve problems involving text manipulation and pattern extraction
- Write a Python function that can decipher hidden messages in acrostic poems
- Test and debug your code to ensure it works correctly
Understand the Problem
In this step, you will learn how to understand the problem statement and the requirements for the Acrostic Poetry Decryption project.
The goal of this project is to write a function acrostic_poetry_decryption(poem) that takes a string representing the content of a poem as input, and returns the decrypted message by extracting the first words of each line and concatenating them together.
The requirements for the project are:
- Except for the
relibrary, do not use any other standard libraries or third-party libraries. - The
poemparameter of the function can be a string that may be empty,None, or any other value. - Do not modify the file path, file name (
acrostic_poetry.py), or function name (acrostic_poetry_decryption(poem)). - When debugging the code in the online environment, use the
python3 acrostic_poetry.pycommand to call Python3.
Implement the Decryption Function
In this step, you will implement the acrostic_poetry_decryption(poem) function to decrypt the message in the acrostic poem.
Here's the starter code for the acrostic_poetry.py file:
import re
from typing import Optional
def acrostic_poetry_decryption(poem: str) -> Optional[str]:
"""
Decipher the message in the acrostic poem by extracting the first words of each line and linking them together.
"""
## If the input content is empty, return `None`
if not poem:
return None
## TODO: Implement the decryption logic
## 1. Split the poem into lines using the ',' or '.' separator
## 2. Extract the first word from each line
## 3. Concatenate the first words together to get the decryption text
## 4. Assign the resulting string to the variable `decryption_text`
return decryption_text
Here's how you can implement the decryption function:
- Split the
poemstring into a list of lines using there.split()function with ther"[,.]"regular expression pattern. - Iterate through the list of lines, and for each line, extract the first word by splitting the line by spaces and taking the first element.
- Concatenate all the first words together to get the decryption text.
- Assign the decryption text to the
decryption_textvariable and return it.
Here's the completed code:
import re
from typing import Optional
def acrostic_poetry_decryption(poem: str) -> Optional[str]:
"""
Decipher the message in the acrostic poem by extracting the first words of each line and linking them together.
"""
## If the input content is empty, return `None`
if not poem:
return None
## Split the poem into lines using the ',' or '.' separator
lines = re.split(r"[,.]", poem)
## Extract the first word from each line
words = [line.strip().split(" ")[0] for line in lines]
## Concatenate the first words together to get the decryption text
decryption_text = "".join(words)
return decryption_text
Test the Decryption Function
In this step, you will test the acrostic_poetry_decryption(poem) function with the example provided in the challenge.
- Add the following code at the end of the
acrostic_poetry.pyfile:
if __name__ == "__main__":
string = "Rain is falling all around, It falls on field and tree. It rains on the umbrella here, And on the ships at sea."
print(acrostic_poetry_decryption(string))
- Save the file and run the following command in the terminal:
python3 acrostic_poetry.py
The output should be:
RainItItAnd
This confirms that the acrostic_poetry_decryption(poem) function is working as expected.
Congratulations! You have completed the Acrostic Poetry Decryption project. If you have any questions or need further assistance, feel free to ask.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



