Answer to Question #322497 in Python for Jolens

Question #322497

B. Create a program using Python programming language that counts election vote for a president. Your program should display first the list of people who are running for president, after which your program should ask the user for his/her vote (based on number). There should be a total of 20 voters. After entering the votes of 20 voters, your program should display the winner including the number



29 / 29



of votes. It should also display the second, third until last place including the number of votes they have. Used for loop and if, elif, else statement on your program. Apply iter() and next() methods on your program. Use list in storing the votes. Observed correct variab le names.




1
Expert's answer
2022-04-03T18:31:24-0400
presidential_candidates = ('Joe Biden', 'Donald Trump', 'Eric Adams', 'Sherrod Campbell Brown', 'Steve Bullock')
votes_summary = []
num = 1
for candidate in presidential_candidates:
    print(num, candidate)
    num += 1
    votes_summary.append(0)
votes = []
for num in range(20):
    vote = int(input(str(num+1)+'> '))
    votes.append(vote)
    votes_summary[vote - 1] += 1
result = []
for i in range(3):
    num = 0
    num_max = 0
    max = 0
    for votes in votes_summary:
        if num in result:
            num += 1
        else:
            if votes > max:
                max = votes
                num_max = num
            num += 1
    result.append(num_max)
print(result)
min = votes_summary[0]
num_min = 0
num = 0
for votes in votes_summary:
    if votes < min:
        min = votes
        num_min = num
    num += 1
print('Winners:')
res = iter(result)
for i in range(1, 4):
    num = next(res)
    print(i, presidential_candidates[num], votes_summary[num])
print('Loser:')
print(presidential_candidates[num_min], votes_summary[num_min])

1 Joe Biden

2 Donald Trump

3 Eric Adams

4 Sherrod Campbell Brown

5 Steve Bullock

1> 5

2> 4

3> 3

4> 2

5> 1

6> 4

7> 3

8> 2

9> 1

10> 3

11> 2

12> 1

13> 2

14> 1

15> 1

16> 1

17> 1

18> 1

19> 1

20> 1

[0, 1, 2]

Winners:

1 Joe Biden 10

2 Donald Trump 4

3 Eric Adams 3

Loser:

Steve Bullock 1


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS