Назначение записывается в строке документации моих ПРОФ:Обновление значения Dict в Python
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, но я не уверен, что это такое?
Действительно ли это ваш отпечаток? – jamylak
Ваш отступ ошибочен. – hjpotter92
Нет, это не мой отпечаток. –