繁体   English   中英

计算输入的候选人数量 - Python

[英]Counting votes for an inputted amount of candidates - Python

代码一直按预期工作,直到最后一个块,我无法弄清楚如何计算每个候选人收到多少票以及有多少没有投票。

#allow tutor to enter name of the tutor group
validNames = ['7A','7B','7C','7D','7E','7F','8A','8B','8C','8D','8E','8F','9A','9B','9C','9D','9E','9F','10A','10B','10C','10D','10E','10F','11A','11B','11C','11D','11E','11F',]
tutorName = input('Enter tutor group name: ')
while tutorName not in validNames:
    print('Invalid tutor group name')
    tutorName = input('Enter tutor group name: ')
#allow the tutor to enter the number of students in the tutor group
tutorgroupnum = int(input('Enter number of students in the tutor group: '))
while tutorgroupnum <28 or tutorgroupnum >35:
    print('Too many or too few ')
    tutorgroupnum = int(input('Enter number of students in the tutor group: '))
#allow the tutor to enter the number of candidates in the election (max four)
candidate_num = int(input('How many candidates?: '))
while candidate_num <1 or candidate_num >4:
  print('Too many or too few')
  candidate_num = int(input('How many candidates?: '))
#allow the tutor to enter the names of the candidates and store in a suitable data structure
candidates_list = ['None']
for i in range(candidate_num):
  candidate = input('Enter the name of the candidate: ')
  candidates_list.append(candidate)
print(candidates_list)


#allow each student to input their vote or abstain
for loop in range(tutorgroupnum):
  vote_input = input('Enter who would like to vote for: ')
  while vote_input not in candidates_list:
      print('Invalid candidate')
      vote_input = input('Enter who would like to vote for: ')

#count the votes for each candidate and student absentions

#after all votes, display name of tutor group, votes for each candidate and the  name of the winner

这可以让你走上正轨:

#allow tutor to enter name of the tutor group
validNames = ['7A','7B','7C','7D','7E','7F','8A','8B','8C','8D','8E','8F','9A','9B','9C','9D','9E','9F','10A','10B','10C','10D','10E','10F','11A','11B','11C','11D','11E','11F',]
tutorName = input('Enter tutor group name: ')
while tutorName not in validNames:
    print('Invalid tutor group name')
    tutorName = input('Enter tutor group name: ')
#allow the tutor to enter the number of students in the tutor group
tutorgroupnum = int(input('Enter number of students in the tutor group: '))
while tutorgroupnum <1 or tutorgroupnum >5:
    print('Too many or too few ')
    tutorgroupnum = int(input('Enter number of students in the tutor group: '))
#allow the tutor to enter the number of candidates in the election (max four)
candidate_num = int(input('How many candidates?: '))
while candidate_num <1 or candidate_num >4:
  print('Too many or too few')
  candidate_num = int(input('How many candidates?: '))
#allow the tutor to enter the names of the candidates and store in a suitable data structure
candidates_list = ['None']
for i in range(candidate_num):
  candidate = input('Enter the name of the candidate: ')
  candidates_list.append(candidate)
print(candidates_list)

can_dict = {x:0 for x in candidates_list}

#allow each student to input their vote or abstain
for loop in range(tutorgroupnum):
  vote_input = input('Enter who would like to vote for: ')
  try:
    can_dict[vote_input] += 1
  except:
       print('Invalid candidate')
  while vote_input not in candidates_list:
      print('Invalid candidate')
      vote_input = input('Enter who would like to vote for: ')
      try:
        can_dict[vote_input] += 1
      except:
          print('Invalid candidate')

print(can_dict)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM