作业由我的教授写在文档字符串中:
def evaluateBallot (voterPreferences, candidates):
"""
Using voterPreferences (a dict associating each voter with a list of
candidate names ordered from highest to lowest preference) and
candidates(a set of candidates still remaining in the election),
returns the vote distribution: a dict associating the name of each
candidate in the election to the number of votes that they received
>>> result = evaluateBallot(dict(x=['a','b','c'], y=['a','c','b'],z= ['c','a','b']),set(['b','c']))
>>> (result['b'],result['c'])
(1, 2)
"""
d ={}
for candidate in candidates:
d[candidate] = 0
for voterPreference in voterPreferences:
if candidate == voterPreference[0]:
d[candidate] += 1
return d
当我运行我编写的代码时,字典不会在每次候选人是选民的首选时更新 +1。我觉得错误出现在我的 if 语句中,但我不确定它到底是什么?