Why does the order of the arguments matter?

Why are the arguments flipped around when it comes to the .format() code? According to codecademy, the top is incorrect, and the bottom is correct. Why, though?

Originally I wrote it this way:

def poem_title_card(title, poet):
  return "The poem \"{}\" is written by {}".format(title, poet)

I’m very confused. This is the code that codecademy gave as the solution:

def poem_title_card(poet, title):
  poem_desc = "The poem \"{}\" is written by {}".format(title, poet)
  return poem_desc

codecademy didn’t flip the arguments around in the example, so I don’t understand why this is happening now.

They are flipped around because of the order in which they are interpolated in the string. TITLE, first, then, POET. It doesn’t matter what order they are given in the parameter so long as the arguments in the call expression match the correct variable.

If the parameters are written,

(title, poet)

then arguments must be,

("I Hear America Singing", "Walt Whitman")

else if written as such,

(poet, title)

then the arguments must be,

("Walt Whitman", "I Hear America Singing")

Either way won’t matter to the output string. The format arguments are ordered according to how they present.

Now in a case where we wish to have both lists the same, say,

(poet, title)

in the parameters, and,

(poet, title)

in the format arguments, we can use numbers in the placeholder to identify which argument goes where…

'The poem "{2}" is written by {1}.'.format(poet, title)
2 Likes

Instructions say which order the arguments will be provided in to the function

1 Like

Thanks, that’s very useful to know, especially that last bit about placeholders.

I do understand why the format arguments have to be in a certain order (“according to how they present”), but I’m not sure why the arguments in def poem_title_card(title, poet): need to be flipped. Why can it not be def poem_title_card(poem,title)?

1 Like

I can see that, too, but is it that way just because the instructions arbitrarily say so?

I don’t see what makes this example so different from the Santana example, that the order the arguments are provided to the function needs to be vice versa.

Argument order is always significant, that is what says which value is which
It’s the same thing as when you send arguments to str’s format method, the order is what says which is which

So then why does the Santana example need the arguments provided in the opposite order?

santanaexample

Opposite to what?

If you mean that there ought to be consistency between the order of title-author … sure? But python certainly doesn’t care about that

in the original argument, it asks for “song, artist,” and then, after .format(, “song, artist.”

Yet in the example I just provided, which is very similar to the Santana one, I need to flip around the arguments. I don’t need to do that with the Santana example.

furthter response:

Then why wouldn’t Python accept this code as correct?:

def poem_title_card(title, poet):
  poem_desc = "The poem \"{}\" is written by {}".format(title, poet)
  return poem_desc

WHAT IS WRONG WITH THAT CODE?? Python says that it’s wrong.

codecademy is not letting me write anymore messages :confused:

Like you point out, codecademy’s designs of the function signatures are a bit inconsistent but it doesn’t have anything to do with python does it?

They say which order the arguments are provided, and yeah they don’t quite match but I kind of fail to care, just write the functions to the provided specs

Good job hitting that limit. I raised it. I think.

1 Like

yes but I’m new to learning Python, so when these errors occur, it makes me think that I did something wrong. So I could have written what I originally wrote, and it should have been okay?

And thanks for raising the limit. I’m not trying to be a nuisance.

Nah that limit only exists to prevent sudden large quantities of spam.

The order doesn’t match in the specification, but… The easiest way for me to express it is that I’m not bothered by it, and I’m easily bothered by issues is exercises.
You ought to be reading the instructions and matching your code to them… It’s not wrong in some way, just different.
Both of those instructions say which order the values will be fed to your function.

If you were writing a library with many exported function where signatures were similar but had very different ways to use them, like different orders then, yeah that’d be poor design. Here though? Meh.

Point taken, I will follow the instructions as given to me from now on. But just to be clear, the error message at the bottom doesn’t make sense, does it?:

my format arguments are in the correct order, yet look at what it says that my code is returning.

According to that, Walt Whitman is the “title” and “I hear America Singing” is the poet.

No your format arguments are in the wrong order, because the title should come first and then the poet, but you’ve done it the other way around because the first argument provided to the function is the poet

(if you rename your parameters to arg1 and arg2 that makes more sense)

The order they appear in the string shouldn’t be considered related to the order of the parameters

At a glance anyway (not going to bother comparing exactly), the error message seems to match the instructions

1 Like

Thanks, that does make sense.

Wow, it just hit me like a ton of bricks. Ugh, I’m sorry.

Hello everyone! Can someone help me understand why Codeacademy is giving me an error, even though my code returns the right answer. (See screenshot)

The error message appears to have the arguments in the reverse order.

def poem_title_card(poet, title):

In addition to what @mtf pointed out, the instructions say:
image

You shouldn’t have the double quotes " at the beginning and end of your output.