Exercise 8-15 Solution Python Crash Course Chapter 8 : Functions

 

📘 Python Crash Course – Chapter 8: Functions – Exercise 8-15 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


📝 Exercise 8-15

Task:
Put the functions for the example print_models.py in a separate file called printing_functions.py . Write an import statement at the top of print_models.py, and modify the file to use the imported functions

✅ Solution Code:

helper_8_15.py


# this help to solve the exercise 8.15 and 8.16

def your_name(first, last):
    full_name = f"{first} {last}"
    return f'Your name is :{full_name}'

exercise_8_15.py


import helper_8_15
# Import the function from the helper file

print(helper_8_15.your_name("John", "Doe"))

🧠 Code Explanation:

  1. Create a separate module:

    A separate file named helper_8_15.py is created to store reusable functions. This helps in organizing the code and keeping the main file clean and modular.

  2. Import the module:

    The import statement is used to import the helper_8_15 module into the current file. This allows access to all the functions defined in the module.

  3. Call a function from the module:

    The function your_name() is called from the helper_8_15 module using dot notation (helper_8_15.your_name()). This demonstrates how to use a function from an imported module.

  4. Pass arguments to the function:

    The function is called with arguments "John" and "Doe", which are passed to the function defined in the module. The function processes these arguments and performs its task.

🔍 Output:


Your name is :John Doe

📚 Related Exercises from Chapter 8:


Previous Post Next Post

Contact Form