What you'll build
Write one function that builds a greeting, then call it on a whole list of applicants. Your first taste of writing a routine once and reusing it — def, a parameter, and return.
Requirements
The must-do parts. If any are missing, we'll ask you to take another pass.
- Define a function
greet(name)that builds andreturns a greeting likeWelcome, Ama!— usereturn, notprint, inside the function. - Call the function at least twice with different names and print each result.
- Make a list of at least four applicant names and use a
forloop to greet every one of them. - Define the function above the lines that call it, with the body indented under the
def.
Bonus, if you're feeling brave
- Give the function a second parameter — the programme they applied to — and work it into the greeting.
- Return a different greeting for an empty name (someone left it blank), without crashing.
Examples
What your program should look like when it runs. Lines starting with $ are typed by you; the rest is your program.
Example cell runs
Welcome, Ama!
Welcome, Kofi!
Welcome, Yaa!
Welcome, Kwame!Where to start
Copy this scaffold into a new file. You don't have to use it — it's just a friendly nudge.
# Reusable greeting function
def greet(name):
# TODO: build the greeting and RETURN it (don't print here)
pass
applicants = ["Ama", "Kofi", "Yaa", "Kwame"]
# TODO: loop over applicants and print the result of greet(name) for eachHow we'll grade it
Four checks, four points. Three or above is passing — we'll ask you to revise anything we can't tick.
| Check | What we look for | Pt |
|---|---|---|
| It runs | Starts cleanly and prints greetings without errors. | 1 |
| Uses return | The function `return`s the greeting string — it does not just `print` inside. The caller receives the value. | 1 |
| Called in a loop | A real list of names is walked with a `for` loop, and the function is called once per name. | 1 |
| Reads well | Greetings come out one per line, clean, with the right name in each. | 1 |