我有一个带有如下字典的函数:
result = {
"testsUntaken" = ["format", "structure"],
"testsFailed" = []
"testsPassed" = []
"otherstuff" = "otherstuff"
}
这是功能。
def take_tests(result):
"""Recursively runs the tests in the testsUntaken list until it is empty"""
testsList = {
"format": allowed_file,
"structure": allowed_structure
}
if len(result["testsUntaken"]) > 0:
test = result["testsUntaken"][0]
if testsList[test](result):
result["testsPassed"].append(test)
else:
result = rejection(result, test)
result["testsUntaken"].remove(test)
result = take_tests(result)
return result
if __name__ == "__main__":
print("something unrelated")
令我惊讶的是,当我从另一个文件中调用它时,此功能非常有效,但当我尝试运行与“ main ”无关的内容时,我得到:
File "my_file.py", line 56, in take_tests
if len(result["testsUntaken"]) > 0:
TypeError: string indices must be integers
答案 0 :(得分:0)
这是一个警察,但我只是把它重写为一个类,它运作良好。已经在范围内的初始化结果字典似乎没有问题。