There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
I can not complete step 6. When I switch to “view solution” in this particular step the solution shows the following.
I submitted a bug report but I’m not sure if I explained it correctly.
Follow a picture (is this a bug?):
I’m pretty new to this but I think I have it right:
I stumbled on step 6 initially but the difference with step 6 vs the preceding 2 steps is that we are wanting to move away from the order we defined in line 2 of first-second-final destination.
Step 6 asks us:
Call the function trip_planner() again using keyword arguments in this exact order:
first_destination: "Iceland"
final_destination: "Germany"
second_destination: "India"
We can see that the order has changed from first-second-final to first-final-second. If you scroll up to the intro before the task on the left side, you will see the explanation about keyword argument which is what we are doing here because the way you have typed it in line 9 would attribute Iceland as first destination (correct), Germany as second and India as second, both of which are incorrect as we are wanting to switch the order.
I’m not completely sure as to the utility of this and what order they would print in.
Does your final output look like this?
“Here is what your trip will look like!
First, we will stop in France, then Germany, and lastly Denmark
Here is what your trip will look like!
First, we will stop in Denmark, then France, and lastly Germany
Here is what your trip will look like!
First, we will stop in Iceland, then India, and lastly Germany
Here is what your trip will look like!
First, we will stop in Brooklyn, then Queens, and lastly Codecademy HQ”
Well explained, thanks!
I was actually able to understand my error later on. As you said, the purpose is for us to apply what we learned - set value to function argument. It is not relevant at this stage but later on, when utilizing multiple arguments kind of make sense, feels useful.
I was thinking the same thing when i wrote it like you did, but i believe its not a bug they probably wrote in their backend something like if bla bla == bla bla then pass is True, but since you wrote it like the above exercisement(assignment 4 and 5) it was returning a false hence the fail.
What there intention was for this exercise was that you implemented the following arguments.
trip_planner(first_destination = “Iceland”, final_destination = “Germany”, second_destination = “India”)
this lies in the word they used: in this exact order:
I got the exercises right but my question is how would it look like if I have defined, for example, the second parameter in the definition but not the first and third and then I call the positional argument for only the first and third
I think I understood what you’re trying to say here. You’ve passed an argument (i.e assigned a value) to one of the parameters, but not to the other two.
In thise case, Python would throw an error, because while defining the function in itself, you did not assign any default values to the other parameters, and Python did not have any value to fallback on - and hence not completely run the function. Here’s an example, where I had defined a default value for argument3, but did not pass all the values.
def trip_planner(first_destination, second_destination, final_destination = "Codecademy HQ"):
print("Here is what your trip will look like!")
print("First, we will stop in "+first_destination + " then "+second_destination + ", and lastly "+final_destination)
trip_planner("France","Germany","Denmark")
Here’s my question:
While defining my function, I’ve given final_destination a value (Here, = “Codeacademy HQ”) which is default, and something that can be picked up if there’s no argument passed for it in subsequent steps.
Now you’d see later, that using the concept of positional argument, I have indeed passed the argument of “Denmark” as the value of final_destination (since both are 3rd in their lists) - However, how is Python smart enough to know that it is an argument for a parameter which is just final_destination?
You see, until I had defined the function, Python didn’t have knowledge of that parameter. Now that I’ve given it, how is it smart enough to know that the parameter is just “final_destination” and not `final_destination = “Codeacademy HQ”’
This is a little basic question as I’m really trying to understand how a computer is smart enough to differentiate that it can accept another value that is different (Denmark) from a value that is assigned to it when it was defined in the first place.(Codeacademy HQ)
Default arguments act more like a fallback, if that parameter is not passed as an argument in a call to the function then the default is used. Under the hood just a little that default value "Codecademy HQ" is actually saved as part of the function object. The parameter name is separate to this value, if an argument is passed to this parameter then that object is assigned to that name in the function, if not then the default is assigned (this occurs on every new call).
Bear in mind that Python is high-level language. There are quite a few levels of abstraction between the code you write and the instructions finally sent to the machine. So it’s not necessary the computer making the choice, it’s the Python interpreter which is itself software that has been written to act in a certain way.
What you have done here is provide the function with a default argument. What your code will do is print out those three locations by default if no argument is provided when the function is called, i.e. trip_planner(). While it works to return a correct answer, it’s not providing a keyword argument.
Providing a keyword argument would look like this:
def trip_planner(first_destination, second_destination, final_destination = "Codecademy HQ"):
print("Here is what your trip will look like!")
print("First, we will stop in " + first_destination + ", then " + second_destination + ", and lastly " + final_destination)
trip_planner(first_destination="Iceland", final_destination = "Germany", second_destination = "India")
Notice how I’m calling the the function on the 5th line after it’s been defined above. If I left first_destination or second_destination blank it would result in an error, but if I leave final_destination blank it will default to “Codecademy HQ”
my output has been without spaces despite following the code, using the + sign.
First, we will stop inLondon, thenIndia, and lastlyNew Zealand
Here is what your trip will look like!
First, we will stop inFrance, thenGermany, and lastlyDenmark
Why is there no space as a result ? what am i missing?
Hi, I experimented with letting the second parameter having the default argument instead of the last parameter, and I got this error. Does this mean that only the last parameter can have the default argument?
It’s not strictly the last parameter (since you can have multiple positional parameters and multiple default parameters), you just need to define all the normal positional parameters (no defaults) before any default parameters.
def f(a, b, c, d='d'): pass # fine
def f(a, b='b', c='c', d='d'): pass # fine
def f(a='a', b, c, d): pass # not valid, error thrown
This lesson (lesson 8) should be lesson 7, and lesson 7 should be lesson 8. Confused why being taught the different arguments after doing a lesson where I was guessing on the order of how things needed to be done.
im stuck the same ■■■■ thing, its a bug for no reason
and im getting fed up of this!
def trip_planner(first_destination, second_destination, final_destination=“Codecademy HQ”):
print(“Here is what your trip will look like!”)
print("First, we will stop in " + first_destination + ", then " + second_destination + ", and lastly " + final_destination)