We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Refactor

Refactoring is a fancy word for improving your code's structure without changing its behavior.

There are different kinds of refactoring. Here, we'll split the code in main.py across two smaller modules.

Importing

In Python, each .py file is a module. You can bring functions, variables, and classes from one module into another with an import statement. A module's name is just its filename without the .py extension.

For example, a database.py module might contain a connect function and a db_version variable. You could import both into main.py:

from database import connect, db_version

We'll organize BookBot with this structure:

  • main.py: The program's entry point and file-reading logic
  • stats.py: Functions that analyze the text

Assignment

  1. from stats import get_num_words
    

Run and submit the CLI tests from the root of your project.