簡體   English   中英

我無法正確調用我的 python function

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

當我嘗試從另一個 python 代碼文件調用 function 時,我收到此錯誤:

回溯(最近一次通話最后):

文件“%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'

等等(完整的代碼在這里

我嘗試了很多不同的方法來讓它工作,但我無法理解,我什至嘗試將它放在PyPI

你需要做一些特別的事情嗎?

我完全被困住了

似乎你混合functionclass ...

從您的代碼中,您想用顏色打印,所以您定義了一個 function,但沒有參數。 也許你想要一個 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)

然后可能是 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM