F-string works now!

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"

highlighted_poems_list = highlighted_poems.split(",")


highlighted_poems_stripped = []

for poems in highlighted_poems_list:
  highlighted_poems_stripped.append(poems.strip())
  
highlighted_poems_details = []
for i in highlighted_poems_stripped:
  highlighted_poems_details.append(i.split(":"))


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

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

for poem in highlighted_poems_details:
  poem_string ="The poem {title} was published by {poet} in {date}".format(title=poem[0],poet=poem[1],date=poem[2])

print(poem_string)
  

Hi, I am currently struggling on the last part of the review excericise: 10.

Finally, write a for loop that uses either f-strings or .format() to prints out the following string for each poem: “The poem TITLE was published by POET in DATE”

The output is only the last set of information in the highlighted_poems_details list. What Im not sure is how come the only output is only the last set of information and not any others within the list?

2 Likes

which won’t work in the LE. Wrong version.

3.5.2 (default, Nov 12 2018, 13:43:14)

This is because it is only one preserved. The instruction states to iterate over the list and print each item, which one would understand to happen inside the loop.


Funny thing, though. The instruction does not specify which object to iterate, only that we should construct output by the provided template. How might we write the for loop such that this format could be used?

print (str.format(title=x,poet=poets[i],date=dates[i]))

You may need to work on this question after passing (or getting the solution) so we can focus on output, rather than whether it is the solution or not.

You’ve got it correct! Just indent your last print so it’s inside the for loop.

1 Like

I’ve got the following code which curiously seems to work in Visual Studio Code but gives a Syntax error on the last print line when trying to run it on the Codeacademy platform. Is there something wrong with the code? Would appreciate any guidance…

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"


highlighted_poems_list = highlighted_poems.split(",")


highlighted_poems_stripped = [item.strip() for item in highlighted_poems_list]


highlighted_poems_details = []
highlighted_poems_details = [item.split(":") for item in highlighted_poems_stripped ]
print(highlighted_poems_details)

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

titles =[list[0] for list in highlighted_poems_details]
poets =[list[1] for list in highlighted_poems_details]
dates =[list[2] for list in highlighted_poems_details]
print(titles)

for i in range(len(titles)):
   print(f"the poem {titles[i]} was published by {poets[i]} in {dates[i]}")
2 Likes

For this exercise we cannot use f-string interpolation since it requires version 3.6 (or 3.7?). The LE version predates that. Try using .format().

Since you are using comprehensions to build the lists, those three lines are not needed.

Hi, guys, for some reason task-10 is getting completed if you hit “Run” no matter what code you wrote after completing task-9. I faced this bug, while trying to intentionally screw up and get the solution.
The other thing is, i didn’t quite remember learning f-string method and task-10 offers to use it.

With help of @mtf i finally get how to combine lists we previously created with iteration through them.

for i in range(len(titles)):
  title = titles[i]
  poet = poets[i]
  date = dates[i]
  glorious_output_string = "The poem \"{title}\" was published by {poet} in {date}.".format(title=title, poet=poet, date=date)
  print(glorious_output_string)

Thanks! :nerd_face:

1 Like

This was not introduced until Python 3.6 and will not work in earlier verrsions. The LE may be Python 3.5.

I’am a bit confused, am i expected to know this method?

What does LE stand for?

Learning Environment

Study up on all three methods of string interpolation. If you are unable to use the f-string, then assume the version of Python is before Python 3.6. It won’t matter which method you use in most exercises.

1 Like

I want to study up all three methods! :grinning: But LE gave me just .format and .format with KEY words

I just don’t get it. Why offer a student this f-string method he could use, but not teach him how. :thinking:

1 Like

That’s a question for CC. Can’t possibly answer it from where we stand (volunteers on the forum).

3 Likes

glorious_output_string I’m using this forever hahaha

Yeah man, this is extremely demotivating, they could at least describe syntax for it :confused:

I thought you guys are working for CC and getting paid for it. First of all let me just say how absolutelly gratefull I am for all the work you are doing, you are life savers :slight_smile: Another thing is that for how much they want for access to lessons least CC could do is employ some people to help us with code. I had couple of question one time and sent them to their official support email, the one that is supposed to be used for troubles with exercises. Their answer? Go to discuss, there are people who will help you with that.

This is the straw that brakes the camel back, after this month I’M DONE with CC, they don’t even bother to reword descriptions of exercises that (judging by this forum) are problematic to a lot of us (like in the beginning the whole “lst of index”), they refuse to help and make other people to do it for free, this is awfull :frowning:

1 Like

You’re welcome!

You are not the first to believe that we are paid staff. Perhaps that is something that needs to be addressed in the new member’s startup guide.

Tagging in @alyssavigil for review and recommendations.

1 Like