所以我一直在为我正在做的课程编写脚本,但是我遇到了错误
该程序的基本思想是投票系统,但是在计算出得票最多的人时,该程序会遇到错误,错误显示为
if votes[0] > votes[1] and votes[0] > votes[2] and votes[0] > votes[3]:
IndexError: list index out of range
完整功能在这里:
def getwinner():
if votes[0] > votes[1] and votes[0] > votes[2] and votes[0] > votes[3]:
print("Congratulations candidate",cands[0],"You win")
if votes[1] > votes[0] and votes[1] > votes[2] and votes[1] > votes[3]:
print("Congratulations candidate",cands[1],"You win")
if votes[2] > votes[0] and votes[2] > votes[1] and votes[2] > votes[3]:
print("Congratulations candidate",cands[2],"You win")
if votes[3] > votes[0] and votes[3] > votes[1] and votes[3] > votes[2]:
print("Congratulations candidate",cands[3],"You win")
if votes[0] == votes[1] and votes[0] == votes[2] and votes[0] == votes[3]:
print("We have a tie")
if votes[1] == votes[0] and votes[1] == votes[2] and votes[1] == votes[3]:
print("We have a tie")
if votes[2] == votes[0] and votes[2] == votes[1] and votes[2] == votes[3]:
print("We have a tie")
if votes[3] == votes[0] and votes[3] == votes[1] and votes[3] == votes[2]:
print("We have a tie")
投票被保存到一个名为“ votes”的数组中,候选名称被保存到“ cands”。这些候选名称与“ votes”数组中的投票对齐。但是有人可以解释这个问题吗,还有没有更简单,更短的解决方法呢?谢谢