割り当ては、私の教授によって docstring に書き込まれます。
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 ステートメントにあるように感じますが、正確にはわかりません。