Build a Greeter Module (Solution)
00:00
Let’s tackle this exercise bit by bit. The first part was to create a module called greeter.py
, and that means we need a file named greeter.py
.
00:14
I will save it on my desktop. And that’s basically the first part of the exercise because with this greeter.py
file, we have a greeter
module, but the greeter
module alone is a bit boring.
00:30
And this exercise also asked for a greet()
function
00:39 that should accept a single string parameter,
00:52
and then print a text, "Hello, {name}!"
. So there are curly braces in there. We’ll come to this in a moment.
01:03 At this point, I just write the comments down to remember what I need to do so that you have a helpful note with the code. In general, it’s a good idea to remove the comments after you created it, but while you’re coding, it’s a good reminder for yourself what you want to do or what you need to do, and in this case, what the exercise wants us to do.
01:25
Let’s start with the first one, create a greet()
function. To create a function, you use the def
keyword and then the function identifier with parentheses, colon, and then we add a pass
into the function body for now, because we will tackle the other comments in a moment.
01:48
Let’s save that and see if we can run the greet()
function without an error. In the interactive window on the left side, you can see my Python interactive window, and when I type greet()
, I get a traceback of NameError
, greet
is not defined in IDLE.
02:05 That happens if you are just saving your file, but you are not rerunning the module. So let’s do that just to make sure I saved. And then I go to Run, Run Module,
02:19
and you can see a restart message. And if I run greet()
now, then nothing happens, which in this case is a good sign because you also don’t get a NameError
.
02:30
That means you’ve defined the greet()
function.
02:34
So that means we can remove that comment, create a greet()
function, because we did this. And now let’s tackle the comment that this greet()
function should accept a single string parameter named name
. To define a parameter for a function, you add it inside of the parentheses.
02:54
So in this case, it’s an opening parenthesis, name
, and then the closing parenthesis. When I save the function now and run it again,
03:11
if I try to run the greet()
function without any arguments, again, I will get an error. This time, it’s a TypeError: greet() missing 1 required positional
argument: name
.
03:23
So that’s good. So that means it accepts a single string parameter named name
.
03:29
If we call this function with a string—for example, "Tappan"
, and then run the greet()
function, then there is no error again. Perfect. So that means it accepts the single string parameter named name
, but it doesn’t print a text "Hello, {name}!"
just yet.
03:49
So let’s tackle this next. Remove the pass
that was in the function body so far. The pass
keyword just says, like, do nothing, more or less.
04:01
So we want to do something, and that’s printing the text Hello
and then the name that was passed in. You call the print()
function inside the greet()
function.
04:11
So make sure that this part in the function body is indented. And then you create an f-string. So f"Hello,
and now come the curly braces with the variable name
in it, so that way you form the string with the name
variable and whatever you pass in as a variable value for name
will get printed out.
04:37
Let’s put an exclamation mark at the end. Let’s check it out, how that works. We save and run the module, and if we now call greet()
with a string—for example,
04:56
"Bartosz"
, then the output is Hello, Bartosz!
, and that’s all that we needed to do in this exercise. I will remove the comment from line 4 so that there is only the function greet()
in the greeter.py
file.
Become a Member to join the conversation.