我如何让这个python函数返回一个值?

时间:2015-09-19 02:28:45

标签: python

我有一个我在python(test.txt)中阅读的文件。示例文件内容如下。

cool let's/cool
hi let's/hi
somek let's/somek

我正在寻找酷,如果它存在于此文件中,我试图获取值让我们很酷

我试过这个

file = open('test.txt')
my_value='cool'

def test_function(my_value):
    for line in file:
        #fields = line.strip().split()
        if line.find(my_value) != -1:
            ddda=line.rsplit(" ",1)[1]

            print(ddda)  ###This is giving my value let's/cool

while true:
    b=test_function(my_value)
    print(b) ##not able to fetch the value of ddda here

如何在我的while循环中存储ddda的值。 没有使用功能有没有更好的做事方式? 我没有使用任何外部模块。

2 个答案:

答案 0 :(得分:3)

print(ddda)  ###This is giving my value let's/cool

这不会给你任何东西。它是打印。如果您想返回一个值,那么return就是print,而不是return ddda

print(b)

执行此操作将允许您Mat MultiplyBlend(const Mat& cvSource, const Mat& cvBackground) { // assumption: cvSource and cvBackground are of type CV_8UC4 // formula: (cvSource.rgb * cvBackground.rgb * cvSource.a) + (cvBackground.rgb * (1-cvSource.a)) Mat cvAlpha(cvSource.size(), CV_8UC3, Scalar::all(0)); Mat input[] = { cvSource }; int from_to[] = { 3,0, 3,1, 3,2 }; mixChannels(input, 1, &cvAlpha, 1, from_to, 3); Mat cvBackgroundCopy; Mat cvSourceCopy; cvtColor(cvSource, cvSourceCopy, CV_RGBA2RGB); cvtColor(cvBackground, cvBackgroundCopy, CV_RGBA2RGB); // A = cvSource.rgb * cvBackground.rgb * cvSource.a Mat cvBlendResultLeft; multiply(cvSourceCopy, cvBackgroundCopy, cvBlendResultLeft, 1.0 / 255.0); multiply(cvBlendResultLeft, cvAlpha, cvBlendResultLeft, 1.0 / 255.0); delete(cvSourceCopy); // invert alpha bitwise_not(cvAlpha, cvAlpha); // B = cvBackground.rgb * (1-cvSource.a) Mat cvBlendResultRight; multiply(cvBackgroundCopy, cvAlpha, cvBlendResultRight, 1.0 / 255.0); delete(cvBackgroundCopy, cvAlpha); // A + B Mat cvBlendResult; add(cvBlendResultLeft, cvBlendResultRight, cvBlendResult); delete(cvBlendResultLeft, cvBlendResultRight); cvtColor(cvBlendResult, cvBlendResult, CV_RGB2RGBA); return cvBlendResult; }

答案 1 :(得分:0)

好吧,返回那个值:

print(ddda)
return ddda