Return 已在 python 3.3 中命名的列表

Return a list that's already been named in python 3.3

我正在尝试 return 从一个函数中获取一些东西,但我一直收到一个错误,说 scoreDict 没有定义,那么代码应该解释我正在尝试做什么..

names = ['joe','tom','barb','sue','sally']
scores = [10,23,13,18,12]

def makeDictionary(names, scores):
    scoreDict = dict(zip(names, scores))
    return scoreDict

print(scoreDict)

提前致谢!

你需要调用那个函数,否则它的主体永远不会执行!

此外,您需要分配它的结果——它不会隐式完成。

所以,添加

scoreDict = makeDictionary(names, scores)

在尝试 print 之前说 scoreDict(或用它做任何其他事情:-)。

你没有调用函数,所以变量scoreDict没有定义在当前范围内:

names = ['joe','tom','barb','sue','sally']
scores = [10,23,13,18,12]

def makeDictionary(names, scores):
    scoreDict = dict(zip(names, scores))
    return scoreDict

scoreDict = makeDictionary(names, scores)
print(scoreDict)

正如 Alex Martelli 所说,您必须调用该函数。解决此问题的一个示例是将 print(scoreDict) 行替换为 print(makeDictionary(names, scores))