当我尝试从另一个python代码文件调用函数时,出现此错误:
追溯(最近一次通话): 文件“%filepath%”,模块第2行 打印(颜色(红色)+“测试”) NameError:未定义名称“红色”
从此代码:
from hcolours import *
print(colours(red) + "Test")
当我在代码中定义它时:
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 is here)
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
您需要做些特别的事情吗?
我完全被困住了
The visibility of the
red
variable you defined incolours
is limited to the function only. To make it visible outside of the function, use theglobal
statement in your function:这样就可以了!