wk02.p13Practice exerciseWeek 02 · Decisions and structureOpen · due next week

Read the function

Ten short Python functions, already written for you. For each one, answer four questions in plain words — its name, what it needs, its job, and whether it returns an answer. No code to write or run; this is pure reading, to make functions click before you write your own.

What you'll build

Ten short Python functions, already written for you. For each one, answer four questions in plain words — its name, what it needs, its job, and whether it returns an answer. No code to write or run; this is pure reading, to make functions click before you write your own.

Requirements

The must-do parts. If any are missing, we'll ask you to take another pass.

  1. For each of the ten functions under The functions below, answer these four questions: (1) What is the name of the function? (2) What information does it need — its inputs? (3) What job does it do, in one plain sentence? (4) Does it return an answer — yes or no — and if yes, what?
  2. Number your answers 1 to 10 so they line up with the functions.
  3. Watch question 4 closely. Some functions return a value the program can use; others only print something and hand nothing back. Telling those two apart is the whole point of this exercise.
  4. Use your own words. Don't copy the code line back — say what it does.
Bonus, if you're feeling brave
  • For any function that only prints, write one extra line saying what it *would* hand back if you changed it to use return.
  • Find the function that walks a list with a loop, and say in one line how its answer changes if the list is empty.

The functions

Read each one, then answer the four questions for it. You are not running any of this code — just reading it.

Function 1
def say_hello():
    print("Hello and welcome to the shop.")
Function 2
def greet(name):
    print("Welcome,", name)
Function 3
def double(number):
    return number * 2
Function 4
def add_prices(price1, price2):
    total = price1 + price2
    return total
Function 5
def make_receipt_line(item, price):
    return item + ": GHS " + str(price)
Function 6
def verdict(score):
    if score >= 50:
        return "Pass"
    else:
        return "Fail"
Function 7
def check_age(age):
    if age >= 18:
        print("You are an adult.")
    else:
        print("You are a minor.")
Function 8
def apply_discount(price, percent):
    discount = price * percent / 100
    return price - discount
Function 9
def total_fees(fees):
    total = 0
    for fee in fees:
        total = total + fee
    return total
Function 10
def count_passes(scores):
    passes = 0
    for score in scores:
        if score >= 50:
            passes = passes + 1
    return passes

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
Names and inputsEach function's name and the information it needs (its parameters) are identified correctly.1
Jobs describedEach function's job is explained in plain words, not just the code repeated back.1
Return or notEach answer correctly says whether the function returns a value (and what) — including the ones that only print.1
All ten, clearlyAll ten are covered, numbered, and easy to follow.1
Ready?
Hand it in
You can submit a draft and revise later if you're not done.
Begin submission →