简体   繁体   English

我无法正确调用我的 python function

[英]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 ...似乎你混合functionclass ...

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.

相关问题 我不知道如何让这个 function 正常工作 - I can't figure out how to make this function work properly 我无法让我的python加密程序正常工作 - I can't get my python encryption program to work properly 我不能在 python 中的 function 中调用 function 吗? - Can't I call function in function in python? 我的 python 街机游戏中的重启功能无法正常工作 - Restart function in my python arcade game doesn´t work properly 我计算最大二进制间隙的函数不起作用,但我不知道为什么 - My Function To Count The Largest Binary Gap Doesn't Work But I Can't Figure Out Why 我无法让带有累加器的循环正常工作,抛出错误 Python - I can't get my loop with accumulator to work properly, throwing error Python 使用 python 打印 JSON 文件时,我的递归函数无法正确放置选项卡。 我该如何解决? - My recursive function won't properly put tabs when printing out a JSON file when using python. How can I fix it? 无法让我的异步功能正常工作 - Can't get my async function to properly work 我怎样才能正确模拟这个 python function? - How can I mock out this python function properly? 我无法弄清楚为什么我的 python 脚本可以运行,但是当尝试使其成为可执行文件时它不起作用 - I can't figure out why my python script works but when try to make it an executable it does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM