Python Sting Methods review - last question error

https://www.codecademy.com/courses/learn-python-3/lessons/string-methods/exercises/review-ii

Hi all,

I’m stuck on the final question (10) in the Python Course–> Strings–>String Methods–>Review and wondered if anyone could help?

My final answer - to question 10 - has been marked as correct but is printing far too much - I’ve clearly got the loops wrong somehow but can’t see how(??)

This what I’ve written (thank you in advance):

highlighted_poems = “Afterimages:Audre Lorde:1997, The Shadow:William Carlos Williams:1915, Ecstasy:Gabriela Mistral:1925, Georgia Dusk:Jean Toomer:1923, Parting Before Daybreak:An Qi:2014, The Untold Want:Walt Whitman:1871, Mr. Grumpledump’s Song:Shel Silverstein:2004, Angel Sound Mexico City:Carmen Boullosa:2013, In Love:Kamala Suraiyya:1965, Dream Variations:Langston Hughes:1994, Dreamwood:Adrienne Rich:1987”

print(highlighted_poems)
highlighted_poems_list = highlighted_poems.split(",")
print(highlighted_poems_list)

highlighted_poems_stripped =

for poem in highlighted_poems_list:
highlighted_poems_stripped.append(poem.strip())
print(highlighted_poems_stripped)

highlighted_poems_details =

for poem in highlighted_poems_stripped:
highlighted_poems_details.append(poem.split(":"))

titles =
poets =
dates =

for title in highlighted_poems_details:
titles.append(title[0])
for poet in highlighted_poems_details:
poets.append(poet[1])
for date in highlighted_poems_details:
dates.append(date[2])

result =
for title in titles:
for poet in poets:
for date in dates:
result.append(“The poem {title} was published by {poet} in {date}.”.format(title = title, poet = poet, date = date))

print(result)

Hi, @tera6156177956 - When I see a Python question involving a block of code, I first scan the block to see if any obvious errors jump out, and, if not, I copy and paste it into my IDE, run it, and see what happens.

Unfortunately, with your question, I can do neither, for (1) it is not Python, a language in which indentations are part of the syntax, and (2), it contains symbols that my Python interpreter will reject, such as right-and-left quotes (“ ”) instead of double quotes (" ") , and an unprintable thing in place of empty brackets.

You could make it so much easier for anyone who would like to help by simply using the </> icon that is in the menu bar atop the text box that you open to type in.

Isn’t this better?

for poem in highlighted_poems_stripped:
    highlighted_poems_details.append(poem.split(":"))

Hi patrickd314,

Thank you very much - I wasn’t actually aware of the symbol option you referenced. It’s my first post but more importantly I think my brain was jelly by that point as I’d been staring at the problem for too long. Does the following run? I have ‘blanked out’ (if that is what one calls #) the final print because the answer is quite long. I think what I’ve done wrong somehow is to reference the poem, then every poet and then within that reference every date, but I am struggling to see exactly where I went wrong. (The aim is to print - e.g. - ‘The poem Afterimages was published by Audre Lorde in 1997’ for each poem, which should be a list of eleven sentences.) I don’t suppose it jumps out to you. And thank you again for the advice on how to post code here. It certainly *is * better!

#Codecademy Python String Methods (final question)
#MY ATTEMPT (error on final two sets of for loops)

highlighted_poems = "Afterimages:Audre Lorde:1997,  The Shadow:William Carlos Williams:1915, Ecstasy:Gabriela Mistral:1925,   Georgia Dusk:Jean Toomer:1923,   Parting Before Daybreak:An Qi:2014, The Untold Want:Walt Whitman:1871, Mr. Grumpledump's Song:Shel Silverstein:2004, Angel Sound Mexico City:Carmen Boullosa:2013, In Love:Kamala Suraiyya:1965, Dream Variations:Langston Hughes:1994, Dreamwood:Adrienne Rich:1987"

print(highlighted_poems)
highlighted_poems_list = highlighted_poems.split(",")
print(highlighted_poems_list)

highlighted_poems_stripped = []

for poem in highlighted_poems_list:
  highlighted_poems_stripped.append(poem.strip())
  print(highlighted_poems_stripped)

highlighted_poems_details = []

for poem in highlighted_poems_stripped:
  highlighted_poems_details.append(poem.split(":"))

titles = []
poets = []
dates = []

for title in highlighted_poems_details:
  titles.append(title[0])
for poet in highlighted_poems_details:
  poets.append(poet[1])
for date in highlighted_poems_details:
	dates.append(date[2])

result = []
for title in titles:
  for poet in poets:
    for date in dates:
      result.append("The poem {title} was published by {poet} in {date}.".format(title = title, poet = poet, date = date))
      
#print(result)

So as you suspected your group of nested for loops at the end is the problem.

lets look at what we get if we run this code

count = 0
for title in titles:
  for poet in poets:
    for date in dates:
      count += 1
print(count)

Output: 1331

This means you are appending your sentence to the result list 1331 times!

You might want to consider using a single for loop and the range() function to access the indexes you need from your titles, poets and dates lists on each loop.