Week 16 - Complex Functions: Becoming a Master Chef

Ridwan IbidunniRidwan Ibidunni
9 min read

Hello Future Coders!

Welcome to Week 16 of our Python Adventures! This week, we're becoming master chefs who create amazing recipes (functions) that can work with any ingredients (lists and dictionaries). Remember how we learned about simple functions before? Now we're going to make them SUPER powerful! Ready to cook up some code magic? Let's go!


Objective

  • Learn how to create functions that work with lists and dictionaries

  • Master return values to get results back from functions

  • Discover how to write flexible functions that can handle different types of data

  • Practice building reusable code blocks that make programming easier

  • Create fun projects using complex functions


1. What Are Complex Functions?

Explanation

Remember simple functions like this?

def say_hello():
    print("Hello!")

Complex functions are like super-powered recipes that can:

  • Take ingredients (parameters) and work with them

  • Give you something back (return values)

  • Work with lists, dictionaries, and multiple pieces of data

Analogy: The Master Chef's Kitchen

Imagine you're a master chef:

  1. Simple Recipe: "Make toast" → Always the same result

  2. Complex Recipe: "Make a smoothie with any fruits" → Different results based on ingredients

  3. Smart Recipe: "Take any fruits, blend them, and tell me the color" → Processes ingredients AND gives feedback

That's exactly what complex functions do with data!


2. Functions That Work With Lists

Basic List Function

# Simple function that works with a list
def count_items(my_list):
    """Counts how many items are in a list"""
    total = len(my_list)
    print(f"There are {total} items in your list!")
    return total

# Let's test it!
fruits = ["apple", "banana", "orange"]
games = ["Minecraft", "Roblox", "Fortnite", "Among Us"]

count_items(fruits)    # Output: There are 3 items in your list!
count_items(games)     # Output: There are 4 items in your list!

Function That Finds Things in Lists

def find_favorite(items_list, favorite):
    """Checks if your favorite item is in the list"""
    if favorite in items_list:
        position = items_list.index(favorite) + 1  # +1 because humans count from 1!
        return f"Yes! {favorite} is #${position} in your list!"
    else:
        return f"Sorry, {favorite} is not in your list."

# Test it out!
snacks = ["chips", "cookies", "candy", "popcorn"]
print(find_favorite(snacks, "cookies"))  # Yes! cookies is #2 in your list!
print(find_favorite(snacks, "pizza"))    # Sorry, pizza is not in your list.

3. Interactive Practice #1: List Functions

Exercise A: The Pet Counter

Fill in the blanks to create a function that counts different types of pets:

def analyze_pets(pet_list):
    """Analyzes a list of pets"""
    total_pets = _____(pet_list)  # (a) How do we count items?

    print(f"You have {total_pets} pets!")

    # Count specific pets
    dogs = pet_list.count("dog")
    cats = pet_list.count("cat")

    print(f"Dogs: {dogs}")
    print(f"Cats: {cats}")

    # Return the total for later use
    return _____  # (b) What should we return?

# Test it:
my_pets = ["dog", "cat", "dog", "fish", "cat", "bird"]
total = analyze_pets(my_pets)
print(f"Total pets returned: {total}")

Hints: (a) len, (b) total_pets

Exercise B: The Grade Analyzer

Create a function that analyzes test scores:

def analyze_grades(grades):
    """Analyzes a list of grades and gives feedback"""
    # Calculate average
    total = sum(grades)
    average = total / len(grades)

    # Find highest and lowest
    highest = max(grades)
    lowest = min(grades)

    print(f"Average grade: {average:.1f}")
    print(f"Highest grade: {highest}")
    print(f"Lowest grade: {lowest}")

    # Give encouragement based on average
    if average >= 90:
        message = "Excellent work! Keep it up!"
    elif average >= 80:
        message = "Good job! You're doing well!"
    elif average >= 70:
        message = "Not bad! A little more practice!"
    else:
        message = "Keep trying! You can improve!"

    return message

# Test it:
test_scores = [85, 92, 78, 96, 88]
encouragement = analyze_grades(test_scores)
print(encouragement)

4. Functions That Work With Dictionaries

Basic Dictionary Function

def introduce_person(person):
    """Introduces a person using their information"""
    name = person["name"]
    age = person["age"]
    hobby = person["hobby"]

    intro = f"Hi! My name is {name}. I'm {age} years old and I love {hobby}!"
    return intro

# Create a person
student = {
    "name": "Alex",
    "age": 12,
    "hobby": "coding"
}

introduction = introduce_person(student)
print(introduction)  # Hi! My name is Alex. I'm 12 years old and I love coding!

Function That Updates Dictionaries

def level_up_character(character, points):
    """Levels up a game character by adding points"""
    print(f"Before: {character['name']} has {character['level']} level and {character['health']} health")

    # Level up!
    character["level"] += 1
    character["health"] += points
    character["experience"] += points * 10

    print(f"After: {character['name']} has {character['level']} level and {character['health']} health")

    return character

# Create a game character
hero = {
    "name": "SuperCoder",
    "level": 5,
    "health": 100,
    "experience": 250
}

# Level up the character
upgraded_hero = level_up_character(hero, 20)
print(f"Total experience: {upgraded_hero['experience']}")

5. Interactive Practice #2: Dictionary Functions

Exercise C: The Student Report Card

Complete this function that creates a report card:

def create_report_card(student_info):
    """Creates a report card for a student"""

    name = student_info["_____"]  # (a) Get the student's name
    grades = student_info["grades"]

    # Calculate average
    average = sum(grades) / _____(grades)  # (b) How many grades?

    # Determine letter grade
    if average >= 90:
        letter = "A"
    elif average >= 80:
        letter = "B"
    elif average >= 70:
        letter = "C"
    else:
        letter = "F"

    # Create report
    report = {
        "student": name,
        "average": round(average, 1),
        "letter_grade": letter,
        "status": "Pass" if average >= 70 else "Needs Improvement"
    }

    return _____  # (c) What should we return?

# Test it:
student = {
    "name": "Maya",
    "grades": [88, 92, 85, 90, 87]
}

report = create_report_card(student)
print(f"{report['student']}: {report['average']}% ({report['letter_grade']}) - {report['status']}")

Hints: (a) name, (b) len, (c) report


6. Functions That Return Multiple Things

Returning Multiple Values (Like a Magic Trick!)

def analyze_text(text):
    """Analyzes text and returns multiple pieces of information"""

    word_count = len(text.split())
    char_count = len(text)
    has_exclamation = "!" in text
    has_question = "?" in text

    # Return multiple things at once!
    return word_count, char_count, has_exclamation, has_question

# Test it:
message = "Hello! How are you doing today?"

# Catch all the returned values
words, characters, excited, questioning = analyze_text(message)

print(f"Words: {words}")
print(f"Characters: {characters}")
print(f"Excited: {excited}")
print(f"Questioning: {questioning}")

Returning Dictionaries (Organized Results)

def game_stats(player_actions):
    """Calculates game statistics and returns them organized"""

    jumps = player_actions.count("jump")
    runs = player_actions.count("run")
    attacks = player_actions.count("attack")

    # Return results in a neat dictionary
    stats = {
        "total_actions": len(player_actions),
        "jumps": jumps,
        "runs": runs,
        "attacks": attacks,
        "most_common": max([jumps, runs, attacks])
    }

    return stats

# Test it:
actions = ["jump", "run", "jump", "attack", "run", "jump", "run"]
game_results = game_stats(actions)

print("Game Statistics:")
for action, count in game_results.items():
    print(f"  {action}: {count}")

7. Hands-On Challenge: Build a Library System

Let's create a mini library system using complex functions!

Challenge A: Book Manager

# Our library database
library = []

def add_book(title, author, pages):
    """Adds a new book to the library"""
    book = {
        "title": title,
        "author": author,
        "pages": pages,
        "checked_out": False
    }
    library.append(book)
    print(f"Added '{title}' by {author} to the library!")
    return book

def find_book(title):
    """Finds a book in the library"""
    for book in library:
        if book["title"].lower() == title.lower():
            return book
    return None

def check_out_book(title):
    """Checks out a book from the library"""
    book = find_book(title)

    if book is None:
        return "Sorry, that book is not in our library."
    elif book["checked_out"]:
        return "Sorry, that book is already checked out."
    else:
        book["checked_out"] = True
        return f"You checked out '{book['title']}'! Enjoy reading!"

def library_summary():
    """Shows a summary of all books in the library"""
    total_books = len(library)
    available_books = 0
    checked_out_books = 0

    for book in library:
        if book["checked_out"]:
            checked_out_books += 1
        else:
            available_books += 1

    summary = {
        "total": total_books,
        "available": available_books,
        "checked_out": checked_out_books
    }

    return summary

# Let's test our library system!
add_book("Harry Potter", "J.K. Rowling", 300)
add_book("The Hobbit", "J.R.R. Tolkien", 250)
add_book("Wonder", "R.J. Palacio", 200)

print(check_out_book("Harry Potter"))
print(check_out_book("The Hobbit"))

stats = library_summary()
print(f"\\nLibrary Summary:")
print(f"Total books: {stats['total']}")
print(f"Available: {stats['available']}")
print(f"Checked out: {stats['checked_out']}")

8. Functions with Default Values (Smart Defaults!)

What Are Default Values?

Sometimes we want our functions to work even if someone forgets to give us all the information!

def create_character(name, health=100, level=1, weapon="sword"):
    """Creates a game character with default values"""
    character = {
        "name": name,
        "health": health,
        "level": level,
        "weapon": weapon
    }

    print(f"Created {name} - Level {level} with {health} health and a {weapon}")
    return character

# Different ways to use it:
hero1 = create_character("Alex")  # Uses all defaults
hero2 = create_character("Maya", 150)  # Custom health, other defaults
hero3 = create_character("Sam", 120, 5, "magic wand")  # All custom values

Interactive Practice with Defaults

Fill in the missing default values:

def plan_party(theme, guests=_____, food="pizza", games=_____):  # (a) & (b)
    """Plans a party with default options"""
    party = {
        "theme": theme,
        "guests": guests,
        "food": food,
        "games": games
    }

    print(f"Planning a {theme} party!")
    print(f"Inviting {guests} guests")
    print(f"Serving {food}")
    print(f"Playing {games}")

    return party

# Test different combinations:
party1 = plan_party("birthday")
party2 = plan_party("superhero", 8, "cake")
party3 = plan_party("space", 6, "sandwiches", "alien tag")

Hints: (a) 5 or 10, (b) "musical chairs" or similar


9. Error-Safe Functions (Remember Our Detective Skills!)

Let's combine what we learned about debugging with complex functions:

def safe_calculator(numbers, operation="add"):
    """A calculator that won't crash!"""
    try:
        # Check if we got a list
        if not isinstance(numbers, list):
            return "Error: Please provide a list of numbers!"

        # Check if list is empty
        if len(numbers) == 0:
            return "Error: List cannot be empty!"

        # Check if all items are numbers
        for num in numbers:
            if not isinstance(num, (int, float)):
                return f"Error: '{num}' is not a number!"

        # Do the calculation
        if operation == "add":
            result = sum(numbers)
        elif operation == "multiply":
            result = 1
            for num in numbers:
                result *= num
        elif operation == "average":
            result = sum(numbers) / len(numbers)
        else:
            return f"Error: Unknown operation '{operation}'"

        return result

    except Exception as e:
        return f"Unexpected error: {e}"

# Test our safe calculator:
print(safe_calculator([1, 2, 3, 4]))           # 10
print(safe_calculator([2, 3, 4], "multiply"))  # 24
print(safe_calculator([10, 20, 30], "average")) # 20.0
print(safe_calculator([1, "hello", 3]))        # Error message
print(safe_calculator("not a list"))           # Error message

10. Quiz Time!

  1. What can complex functions do that simple functions can't?

    • A) Print messages

    • B) Work with lists and dictionaries

    • C) Use variables

  2. What does 'return' do in a function?

    • A) Ends the program

    • B) Gives back a result

    • C) Prints something

  3. What are default values in functions?

    • A) Values used when no parameter is given

    • B) The first parameter

    • C) Error messages

  4. True or False: A function can return multiple values

    • A) True

    • B) False

  5. Which is better for organizing returned data?

    • A) Lists

    • B) Dictionaries

    • C) Strings


13. Recap & Reflection

This week we learned to create super-powered functions that can:

  • Work with lists to process multiple items

  • Handle dictionaries to work with organized data

  • Return results that we can use later

  • Have default values to make them flexible

  • Handle errors gracefully like good detectives

Discussion Prompt:

Think about your daily routine. What are some "functions" you do that take different "inputs" and give different "outputs"? For example, making breakfast might take ingredients (inputs) and give you a meal (output). How could you write a function to describe this?

Next Week Preview:

Get ready for File Handling! We'll learn how to save our data to files and read it back later. It's like giving our programs a memory that lasts forever. You'll be able to create programs that remember things even after they're closed!


0
Subscribe to my newsletter

Read articles from Ridwan Ibidunni directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ridwan Ibidunni
Ridwan Ibidunni

I'm a mathematician that loves its applications in all spheres of life, especially in the field of machine learning. I write Java, Python and Android applications