🐸

Your Own Scroll

Python Basicspython-architect-16-own-scroll
Reward: 100 XP
|

You have mastered using other wizards' scrolls. Now, it is time to write your own.

Great architects don't write all their code in one massive file. They organize it into smaller, reusable scrolls (modules).

Creating a Module

A module is just a Python file ending in .py. If you foster a file named utils.py, you can import utils in your main code!

def power_up():
  print("Power Level: MAX!")
import utils

utils.power_up()
1
Open the Scroll

Look for the tabs at the top of the editor. Click on utils.py to open it.

2
Write the Spell

In utils.py, write a function:

def say_hello(name):
    print("Hello, " + name)
3
Import and Cast

Go back to main.py. Import your new module (import utils) and call utils.say_hello("Hoppy").

The filename matters! utils.py becomes import utils. Don't use spaces in filenames.

Suggested Solution
Expand
Solution:
# File: utils.py
def say_hello(name):
  print("Hello, " + name)

# File: main.py
import utils

utils.say_hello("Hoppy")
Advanced Tips
Want more? Click to expand

F. The Entry Point

  • When you run a file directly, Python sets a special variable __name__ to "__main__".
  • You will often see if __name__ == "__main__": in code. This means "only run this part if I'm the main file, not if I'm being imported."
Loading...
Terminal
Terminal
Ready to run...