[SOLVED] How do I separate this into two files?

Issue

This works altogether, however doesn’t when they are inside separate files, like so:
main.py

from write import *

def write():
    global text
    text = "this is a test"
    colour()

write()

colour.py

import sys
from time import sleep

blue = '\033[0;34m'

def colour():
    for char in text:
        sleep(0.05)
        sys.stdout.write(blue)
        sys.stdout.write(char)
        sys.stdout.flush()
    sleep(0.5)

Error is as follows:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    write()
  File "main.py", line 6, in write
    colour()
  File "/home/runner/Function-Test/colour.py", line 7, in colour
    for char in text:
NameError: name 'text' is not defined

Solution

This is how I would do it if I understand correctly:

write.py

from colour import colour

def write():
    text = "this is a test"
    colour(text)

write()

colour.py

import sys
from time import sleep

blue = '\033[0;34m'

def colour(text):
    for char in text:
        sleep(0.05)
        sys.stdout.write(blue)
        sys.stdout.write(char)
        sys.stdout.flush()
    sleep(0.5)

Result:

this is a test Written slowly in Blue.

Answered By – user56700

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published.