描述:请实现一个计票统计系统。你会收到很多投票,其中有合法的也有不合法的,请统计每个候选人得票的数量以及不合法的票数。(注:不合法的投票指的是投票的名字不存在n个候选人的名字中)数据范围:每组输入中候选人数量满足1<n<100,总票数量满足1<n<100 输入描述:第一行输入候选人的人数n,第二行输入n个候选人的名字(均为大写字母的字符串),第三行输入投票人的人数,第四行输入投票。 输出描述:按照输入的顺序,每行输出候选人的名字和得票数量(以“:”隔开,注:英文冒号左右两边都有一个空格),最后一行输出不合法的票数,格式为“Invalid:”+不合法的票数 根据以上内容编写一个python程序
时间: 2024-04-15 10:30:41 浏览: 97
下面是一个实现计票统计系统的 Python 程序:
```python
def count_votes():
# 输入候选人数量和名字
num_candidates = int(input())
candidates = input().split()
# 初始化候选人得票数量为0
votes = {candidate: 0 for candidate in candidates}
# 输入投票人数量和投票
num_votes = int(input())
all_votes = input().split()
# 统计候选人得票数量
invalid_votes = 0
for vote in all_votes:
if vote in votes:
votes[vote] += 1
else:
invalid_votes += 1
# 输出候选人得票数量和不合法的票数
for candidate, vote_count in votes.items():
print(candidate + ": " + str(vote_count))
print("Invalid: " + str(invalid_votes))
count_votes()
```
你可以使用这个程序来统计候选人的得票数量和不合法的票数。首先输入候选人数量,然后输入每个候选人的名字。接下来输入投票人数量,再输入每个投票的名字,程序将输出每个候选人的得票数量,并在最后一行输出不合法的票数。
阅读全文
相关推荐















