Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

python - How to print multiple winners in a plurality election program?

I am trying to create a program that simulates a plurality election. The code is working fine when there is 1 winner but gets stuck when there's a draw and there are multiple winners. I tried using a if-elif loop but it is not working as expected. It would be helpful if you could modify the code to make it work.

candidate_number = int(input("Number of candidates: "))
while candidate_number <= 0:
        candidate_number = int(input("Number of candidates: "))

candidates = []
votes = []
find = []
tie = []


for i in range(1, 1 + candidate_number):
  a = input("Candidate: ").upper()
  while a in candidates:
      a = input("Candidate: ").upper()
  else:
      candidates.append(a)

print("")
voter_number = int(input("Number of voters: "))
while voter_number <= 0:
    voter_number = int(input("Number of voters: "))


for i in range(1, 1 + voter_number):
    a = input("Vote: ").upper()
    votes.append(a)

for i in range(len(votes)):
   find.append(votes.count(votes[i]))

k = find.index(max(find))
for i in range(3):
    print("")
print("Winner: " + votes[k])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could if there is onle one max value

if find.count(max(find)) == 1:

and then use your old method (for single winter) or for-loop to display all winners.

max_count = max(find)

for c in candidates:
    if votes.count(c) == max_count:
        print('MAX VOTES:', c)

Minimal working code

votes = ['A', 'B', 'A', 'Mr. X', 'B', 'C', 'D', 'C']
candidates = sorted(set(votes))  # create list of candidates base on votes
find = []

# display all candidates
print('--- candidates ---')
for c in candidates:
    print(c)

# count votes and also display it 
print('--- count votes ---')
for c in candidates:
    count = votes.count(c)
    find.append(count)
    print(c, ':', count)
  
# find max count  
print('--- max ---')

max_count = max(find)
print('max_count:', max_count)

print('

--- results ---

')

# check if only one max count
if find.count(max_count) == 1:
    # display winner
    index = find.index(max(find))
    print('Winner:', votes[index])
else:
    # display all 
    for c in candidates:
        if votes.count(c) == max_count:
            print('Draw:', c)

EDIT: x2

if find.count(max_count) == 1:

    # ... code ...

else:
    # display all 

    # --- before loop ---

    all_winners = []

    # --- loop ---

    for c in candidates:
        if votes.count(c) == max_count:
            all_winners.append(c)

    # --- after loop ---        

    print('Draw:', ",".join(all_winners) )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...