Use Arguments
00:00
Let’s recap what the actual task is here after we have investigated this for a bit. So we need to adjust the function definition of visit_woods()
to accept a string as an argument, and then update the function call of visit_woods()
so the program prints the value of my_invitation
.
00:18
Let’s start with the first part and add a parameter to the visit_woods
function, which in this case then should be my_invitation
.
00:31 If you save and then run the file,
00:35
then you get an error: `visit_ woods()` is missing one required positional argument: my_invitation
. So this gives us a hint that when we call visit_woods()
in line eight, we need to pass in something, and that should be the invitation.
00:50
So let’s do that. Pass in invitation
as the argument for the visit_woods()
function call, and save and run the file.
01:01
And then you can see the output: “Let’s have a party!” which is the solution, but there’s also another output, which is good that we have it here because then we can explore what’s currently in the local scope, which is the my_invitation
name, which is the key of the local dictionary with the value: “Let’s have a party!” So that’s basically the invitation that you are passing in to the visit_woods()
function.
01:26
That’s the solution to the exercise. And finally, let’s clean up a bit and remove the print()
function call with locals()
in it so that when you save and run the program, the output is only, “Let’s have a party!” Sounds awesome and sounds right. Perfect.
Become a Member to join the conversation.