我正在尝试运行一个 python 程序,该程序可以从一个文件中运行一个字典,其中包含一个单词列表,每个单词都有一个分数和标准差。我的程序如下所示:
theFile = open('word-happiness.csv', 'r')
theFile.close()
def make_happiness_table(filename):
''' make_happiness_table: string -> dict
creates a dictionary of happiness scores from the given file '''
return {}
make_happiness_table("word-happiness.csv")
table = make_happiness_table("word-happiness.csv")
(score, stddev) = table['hunger']
print("the score for 'hunger' is %f" % score)
我的文件中有“饥饿”一词,但是当我运行该程序以获取“饥饿”并返回其给定分数和标准偏差时,我得到:
(score, stddev) = table['hunger']
KeyError: 'hunger'
即使字典中有“饥饿”,我怎么会得到一个关键错误?