[英]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:当我尝试从另一个 python 代码文件调用 function 时,我收到此错误:
Traceback (most recent call last):
回溯(最近一次通话最后):
File "%filepath%", line 2, in module
文件“%filepath%”,第 2 行,在模块中
print(colour(red) + "Test")
打印(颜色(红色)+“测试”)
NameError: name 'red' is not defined
NameError:名称“红色”未定义
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我尝试了很多不同的方法来让它工作,但我无法理解,我什至尝试将它放在PyPI上
Do you need to do something special?你需要做一些特别的事情吗?
I'm completely stuck我完全被困住了
Seems you mix function
and class
...似乎你混合
function
和class
...
From your code, you want to print with color, so you defined a function, but without argument.从您的代码中,您想用颜色打印,所以您定义了一个 function,但没有参数。 Maybe you want a class here?
也许你想要一个 class 在这里?
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然后可能是 function
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.