Loading video player…

Greet Someone (Solution)

00:00 Welcome to the first exercise. It’s a good idea to post the exercise again as a comment so that you know what you need to work on.

00:10 Since I have a big font size here, I divide the exercise into different lines later. In this course, it’ll also make sense to have it in different lines because it’s kind of like a to-do list that you’re working with here, but this exercise is quite straightforward, so you can see this as one bigger task.

00:30 You need to create a function def greet(): and then in the next line you print "Hello!", and for now, let’s skip the name. Let’s just print("Hello!") and see if that works.

00:44 To run the file with IDLE, you need to save it. So here let’s use greeter.py,

00:53 and when you run the module, you can call greet()

00:58 and the output is Hello!. Okay, so that works. Now you need to pass in the parameter name. So inside the parentheses of the greet() function definition, you add name, and then you go to the print() function call, and then you add curly braces with the {name} variable to the string.

01:18 If you run the module now and you call greet with the name "Sadie", for example, as a string, then you see it doesn’t print quite correctly.

01:29 Can you spot why? Yeah. I forgot the f at the beginning of the string to make it an f-string. Only then the string formats with the variable that you pass in and also the exclamation mark belongs at the end.

01:43 Let’s try it again. If you run greet() and then pass in the string "Sadie",

01:51 then it prints Hello, Sadie! Perfect. That means the exercise is solved and you can remove the comment at the beginning of this file, so you only have the greet() function there.

02:03 Perfect. Let’s move on to the next exercise.

Become a Member to join the conversation.