In this exercise, you're going
to be creating a function
that adds countries that you've visited into a Travel Log.
Take a look in the starting code,
there is a list of dictionaries called travel_log
and it contains keys and values such
as the country that was visited, the number of visits made
and the cities that have been visited in that country.
Now, the goal for you is
without modifying this travel_ log directly
you should create a function called add_new_country
which takes three inputs
the name of the country, the number of visits
and the cities that have been visited in a list.
Take a look in the Description box,
look at the Example Input
and what the Expected Output should look like
and try to create your function.
Remember, you shouldn't need to modify the travel_
log directly, and instead
you should write the function that achieves this purpose
because otherwise when you hit Submit
your code is not going to pass the test.
Look at the instructions carefully and give this a go.
All right hopefully you managed to complete this
and you want to check the solution together with me.
If you didn't manage to complete it and you want to get a hint
or see how I did it, then feel free to follow along
and we can review the solution together.
Now, the first step is to create our function
which is called add_new_country.
Next, we provide the three inputs
to this function that's already been defined on line 24,
that's the name of the country,
the times that have been visited,
and the cities that have been visited.
Now notice I've named the inputs in the function
on line 17 different from what's on line 24
just so that you can tell the difference when
I use it in the function.
In reality
you can call your inputs whatever it is you like,
as long as it works, it's fine.
Now, the next thing we need to do is to create
a new dictionary, which I've called new_country
and the first thing we do is we provide a key and value pair
and put it into this new dictionary.
We can write new_country[ ], inside the square
brackets the key, which is country, and then
we can set it equal to the value, which is the first input
on line 17, the name of the country that has been visited.
Next, we do the same thing with the other two inputs
the number of visits and the cities that have been visited.
So we're just passing the data through the function.
The data comes
into the function, and we can grab it by the names
through the names of the input
on line 17, and we insert them one by one into
this new dictionary that we've created, called new_country.
Finally, all we have to do to add it
to our list is to use the append() method, so we grab hold
of our travel_log list, write .append, and then
inside the parentheses we can add what we want to append
or what we want to add to the end of that list.
And it is of course our new entry
that new dictionary that we created
which is called new_country, and that's all there is to it.
We managed to grab hold of the inputs,
put it into a new dictionary by key and value pairs
and then insert it into the list of our travel_log.
If you need to make any adjustments to your code
go ahead and do that now.