I did exactly what I was asked in this exercise, and it takes my solution for the first part, yet when I try to print it I get NameError publishing_date not defined.
Why is that?
def poem_description(publishing_date, author, title, original_work):
poem_desc = "The poem {title} by {author} was originally published in {original_work} in {publishing_date}.".format(publishing_date=publishing_date, author=author, title=title, original_work=original_work)
return poem_desc
From my understanding, keyword arguments are stored as temporary variables.
Do the keyword args of the function poem_description() not conflict with the keyword args of the string format because they have different scopes?
So, I feel like something really important is missing in the lesson text here. Because all of the keywords and variable names are the same (song and song, publishing_date and publishing_date), it’s not clear for the reader if the keyword or the variable is coming first inside the format().
What is mean is, which of the following is correct?
‘This is {answer}.’.format(answer=variable)
‘This is {answer}.’.format(variable=answer)
My intuition from the other lessons is keyword = variable, but that’s just intuition.
I will post here some example using the author tag as an example:
def poem_description(publishing_date, authorVARIABLE, title, original_work):
poem_desc = "The poem {title} by {authorANSWER} was originally published in {original_work} in {publishing_date}.".format(publishing_date = publishing_date, authorANSWER = authorVARIABLE, title = title, original_work = original_work)
return poem_desc
So the right one is: ‘This is {answer}.’.format(answer=variable).
I could only finish the exercise using the “view solution” button. I found same problem as you. I really think that probably it’s some kind of bug but I don’t have a clue about it.
Is the ability to jumble the order of the arguments in this format mutually exclusive between the string and calling the function?
For example:
my_beard_description = poem_description(“1974”, “Shel Silverstein”, “My Beard”, “Where the Sidewalk Ends”) the order of the parameters as described in the original function will return a correct, response, however if you enter the parameters while calling it as they appear in the exercise:
author = “Shel Silverstein”
title = “My Beard”
original_work = “Where the Sidewalk Ends”
publishing_date = “1974”
This lesson may have been written before f-strings were introduced in 3.6; it’s still important to learn about .format() though, as you’ll likely encounter both f-strings and .format() in others’ code.
Is it not bad that we are using keywords which are the same as the variable names? Like aren’t we just self - referencing the keywords? Isn’t that bad?
I found you don’t even have to write the whole thing out, there is a thing called Formatted string, it looks like this:
def poem_description(publishing_date, author, title, original_work):
poem_desc = f"The poem {title} by {author} was originally published in {original_work} in {publishing_date}."
return poem_desc
my_beard_description = poem_description("1974", "Shel Silverstein", "My Beard", "Where the Sidewalk Ends")
print(my_beard_description)
If you input this into the script, it works the same way.