我无法找到正确调用python函数的方法
当我尝试从另一个python代码文件调用函数时,出现以下错误: 回溯最近一次呼叫上次: 模块中的文件%filepath%,第2行 PrintColorred+测试 NameError:未定义名称“red” 根据该代码:我无法找到正确调用python函数的方法,python,function,Python,Function,当我尝试从另一个python代码文件调用函数时,出现以下错误: 回溯最近一次呼叫上次: 模块中的文件%filepath%,第2行 PrintColorred+测试 NameError:未定义名称“red” 根据该代码: from hcolours import * print(colours(red) + "Test") 当我在代码中定义它时: def colours(): reset = '\033[0m' bold='\033[01m'
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'
以此类推,完整的代码是
我试过很多不同的方法让它工作,但我不明白,
我甚至试过把它挂在墙上
你需要做些特别的事吗
我完全被卡住了看来你把函数和类混在了一起
在代码中,您希望使用颜色打印,因此您定义了一个函数,但没有参数。也许你想在这里上课
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)
然后一个可能的函数可以
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")
红色!='红色“。。。变量red未定义。red不是def颜色的参数,要导入它,请使用以下命令:导入hcolours。colours@FishingCode,我得到:没有名为hcolours.colors的模块,hcolours不是一个包