调用方法时的ValueError

时间:2016-05-04 14:00:08

标签: python python-2.7

这是一个部分代码,应该能够确定某些.c文件中的参数是否未使用。

首先,它找到一个参数名称并将其保存为theParam,然后调用searchAndValueExtract,其中theParam用作搜索词来搜索具有相同名称的参数(然后读取他们的价值观)

import os
savedValue = ""
compareValue = ""
theParam = ""
usedParameters = list()
equalValueCounter = 0
parameterCounter = 0
emptyline = "" 

def searchAndValueExtract(theParam, compareValue, parameterCounter, savedValue):
        # when theParam has a value
        # this method will be called to find an identical parameter and then grab it's value for comparison
        for path, compdirs, compfiles in os.walk('C:/PATH'):
            for compfile in compfiles:
                if compfile.endswith('.c'):
                    with open(os.path.join(path, compfile), 'r') as r:
                        for line in r:
                            if '=' in line and theParam in line:
                                savedValue = value(row, line, savedValue, statements, cur)
                                compareValue = savedValue
                                parameterCounter += 1
                                return compareValue
        return parameterCounter

for root, dirs, files in os.walk('C:/PATH'):
    for cFile in files:
        if cFile.endswith('.c'):
            with open(os.path.join(root, cFile), 'r') as this:
                for row in this:
                    if '=' in row and row != emptyline:
                        theParam = takeTheParam(row, theParam)
                        savedValue = value(row, savedValue, statements, cur, line)
                        while theParam not in usedParameters:  # Has the param already been checked?
                            safeValue = savedValue
                            compareValue, parameterCounter = \
                                searchAndValueExtract(theParam, compareValue, parameterCounter, savedValue)
                            if safeValue == compareValue:
                                equalValueCounter += 1
                                continue
                            else:
                                usedParameters.append(theParam)
                                break
                        else:
                            break

编译时我收到错误

File "C:/PATH/ParamChecker.py", line 82, in <module>
    searchAndValueExtract(theParam, compareValue, parameterCounter, savedValue)
ValueError: need more than 1 value to unpack

我已经四处寻找,但没有任何事情可以帮助我理解它,或者我根本没有理解如何在我的案例中实施解决方案。

1 个答案:

答案 0 :(得分:2)

函数searchAndValueExtract返回单个值,但您尝试将其返回值分配给2个变量。

def searchAndValueExtract(theParam, compareValue, parameterCounter, savedValue):
    ...
        return compareValue
    return parameterCounter

.
.
.

compareValue, parameterCounter = searchAndValueExtract(theParam, compareValue, 
                                                       parameterCounter, savedValue)