简体   繁体   中英

I can't work out a to call my python function properly

When I try to call a function from another python code file, I get this error:

Traceback (most recent call last):

File "%filepath%", line 2, in module

print(colour(red) + "Test")

NameError: name 'red' is not defined

from this code:

from hcolours import *
print(colours(red) + "Test")

When I have it defined in my code:

def colours():    
    reset = '\033[0m'
    bold='\033[01m'
    disable='\033[02m'
    underline='\033[04m'
    reverse='\033[07m'
    strikethrough='\033[09m'
    invisible='\033[08m'
    black='\033[30m'
    red = '\033[31m'

and so on (the full code ishere )

I have tried lots of different ways to get it to work but I can't understand it, I have even tried putting it up on PyPI

Do you need to do something special?

I'm completely stuck

Seems you mix function and class ...

From your code, you want to print with color, so you defined a function, but without argument. Maybe you want a class here?

class colours:    
    reset = '\033[0m'
    bold='\033[01m'
    disable='\033[02m'
    underline='\033[04m'
    reverse='\033[07m'
    strikethrough='\033[09m'
    invisible='\033[08m'
    black='\033[30m'
    red = '\033[31m'

print(colours.red + "Test" + colours.reset)

Then a possbile function could be

class colours:    
    reset = '\033[0m'
    bold='\033[01m'
    disable='\033[02m'
    underline='\033[04m'
    reverse='\033[07m'
    strikethrough='\033[09m'
    invisible='\033[08m'
    black='\033[30m'
    red = '\033[31m'

def print_colours(str):
        print(colours.red + str + colours.reset)
#we can test the string as below
#print_colours("Test")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM