对四名候选人的选举进行计票的简单程序。 一次只能得到一张选票,四票候选人的票数由数字表示, 最后在屏幕上打印出获胜者。你知道吗
这是我的密码
candList = [0, 0, 0, 0]
while True:
print '1 for First Candidate'
print '2 for Second Candidate'
print '3 for Third Candidate'
print '4 for Fourth Candidate'
print '5 for Exit Poll'
cid = input('Enter Candidate Number to Vote: ')
if cid == 5:
break
candList[cid - 1]
vote = max(candList)
candidate = candList.index(vote) + 1
print 'Winner is Candidate', candidate, 'with', vote, 'Votes'
但问题是选票不算在内。。 我给1个候选人3票,但最终打印
Result is : Winner is Candidate 1 with 0 Votes
代码中有许多问题。你知道吗
首先,
id
是一个内置函数,不要使用id
作为变量名。对于list
秒也是如此,第15行(list[id-1]
)显然没有任何作用。第三,不应该使用eval
将字符串转换为整数,而应该使用int
。你知道吗这段代码应该可以完成这项工作,但它仍然有一些警告:用户可以输入15或不是一个数字,程序将被终止,也不处理两个候选人获得相同票数的情况
你从不给
list[cid - 1]
赋值。您应该将该行更改为以下内容:另外,我建议您不要使用
list
作为列表的名称。你知道吗勾选此项您不更改相应的人数更改代码
相关问题 更多 >
编程相关推荐