我正在制作投票软件,并且遇到一些问题。这是一个示例:
import random
One = "Test"
Two = "Testy"
Three = "Testyy"
Four = "Testyyy"
Five = "Testyyyy"
Six = "Testyyyyy"
running = {One: 0, Two: 0, Three: 0, Four: 0, Five: 0, Six: 0}
for voter in running:
vote = random.choice(running)
running[vote] += 1
winner = max(running)
我想做到的是,如果两名候选人并列获胜,它将随机选择其中一名作为获胜者。有什么办法吗?
答案 0 :(得分:1)
您可以找到最大投票数,然后从字典中选择具有该值的键:
max_votes = max(running.values())
winners = [candidate for candidate, votes in running.items() if votes == max_votes]
winner = random.choice(winners)
答案 1 :(得分:-1)
winners=[]
for key,value in running:
if value == winner
winners.append(key)
<pick random person from winners>