Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I separate this into two files?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading