wk02.p11Practice exerciseWeek 02 · Decisions and structureOpen · due this week

Reusable greeting function

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.

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.

  1. Define a function greet(name) that builds and returns a greeting like Welcome, Ama! — use return, not print, inside the function.
  2. Call the function at least twice with different names and print each result.
  3. Make a list of at least four applicant names and use a for loop to greet every one of them.
  4. 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 each

How we'll grade it

Four checks, four points. Three or above is passing — we'll ask you to revise anything we can't tick.

CheckWhat we look forPt
It runsStarts cleanly and prints greetings without errors.1
Uses returnThe function `return`s the greeting string — it does not just `print` inside. The caller receives the value.1
Called in a loopA real list of names is walked with a `for` loop, and the function is called once per name.1
Reads wellGreetings come out one per line, clean, with the right name in each.1
Ready?
Hand it in
You can submit a draft and revise later if you're not done.
Begin submission →