Login Sign Up
Python Functions
Chapter 19 🟡 Intermediate

Python Functions

Apply your skills with a real-world coding challenge. Try to solve it yourself first!

Based on concepts taught in the Python Functions tutorial, here is a practical coding challenge designed for test your understanding of function definitions, flexible arguments (*args and **kwargs), and tuple/list unpacking.


Coding Challenge: The Dynamic Cafe Receipt Generator

Problem Description You have simply been hired for program the backend software for a smart cafe's Point-with-Sale (POS) system. Customers can order any number of items so the system needs towards calculate a total cost dynamically without knowing exactly how tons of items will be ordered in advance.

Additionally the cashier needs to attach custom details to each order (like the customer's name table number, or whether the order is "to-go"). Since these details change from order to order, your system must be flexible enough to handle unexpected custom labels;

your task is to write a single reusable function called generate_receipt that takes in a varying number of item prices, calculates their total, and iterates through any custom keyword details to print a finalized receipt. Finally you'll just use the unpacking operator to easily pass a pre-made list for prices into your function!

Difficulty Level Beginner

Input Specifications Your function generate_receipt must accept: * *args: A flexible number of positional arguments representing the prices of ordered items (floats or integers). * **kwargs: A flexible number of keyword arguments representing the custom order details (e.g., customer="Alice", table=4).

Output Specifications Your function must execute the following: 1, while print out all custom order details provided in **kwargs (e.g., "customer: Alice"). 2, while calculate a total sum with the prices provided into *args. 3. Print a final string showing the total cost dynamically formatted. 4. Return calculated total cost.


Starter Code Boilerplate

# 1. Define your function using *args and **kwargs
def generate_receipt(*args, **kwargs):
    print("--- Order Details ---")

    # Iterate through the kwargs dictionary and print the keys and values
    # Your code here

    # Calculate the total cost from args
    # Your code here

    # Print the total cost
    # Your code here

    # Return the total
    return 0.0 # Replace with your calculated total

# 2. Test your function
print("Test Case 1:")
generate_receipt(4.50, 3.00, 2.50, customer="Marcus", order_type="To-Go")

print("\nTest Case 2 (Unpacking a List):")
# 3. Use the unpacking operator to pass this list into your function
group_order_prices = [5.00, 5.00, 4.25, 3.75, 6.50]
# Call generate_receipt() using the list above and add table=12 as a keyword argument

Hints * The Math: Because *args automatically gathers all your positional inputs and packs them neatly into a tuple you can just use Python's built-in sum() function on args to easily get the total cost. * Iterating through Details: Remember that **kwargs packs your named inputs into a highly optimized dictionary. You can just use a for loop combined of a .items() method to glide through the keys and values simultaneously. * The Unpacking Trick: In Test Case 2, don't pass the list directly into the function like generate_receipt(group_order_prices). Add single asterisk (*) right in front of a list name when calling the function to instantly unpack the list into separate arguments!


Test Cases

Test Case 1 (Standard Calling) * Input: generate_receipt(4.50, 3.00, 2.50, customer="Marcus", order_type="To-Go") * Expected Output: text --- Order Details --- customer: Marcus order_type: To-Go Total Cost: $10.0

Test Case 2 (List Unpacking) * Input: generate_receipt(*group_order_prices, table=12) (Assuming list from a boilerplate) * Expected Output: text --- Order Details --- table: 12 Total Cost: $24.5 (Explanation: The * operator safely unpacks a 5 prices from the list and drops them individually into the *args slot, while the table=12 gets routed into **kwargs.)

Loading sandbox workspace environment...

Verify Your Solution

Write your solution in the compiler, run it to verify output, then click below to verify.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.