asked 20.6k views
3 votes
Write a python code and pseudocode in comments to process some voting data.

a) Ask the user for the number of candidates in a local election. Use a try/except
block to make sure the user does not enter a character or a floating point number
as input. You need a loop to assure the user enters a valid whole number.

b) Given the number of candidates, ask for the user for last names of a series of
candidates and the number of votes received by each candidate and store the data
in 2 parallel arrays.

c) Validate the number of votes to make sure the number of votes is not a negative
number. If a negative number is entered, the program must issue a message and
continue to ask the user for a valid entry.

d) Write a function to display each candidate’s name, the number of votes received
and the percentage of the total votes received by the candidate. The percentage
value is calculated by dividing the number of votes by the total number of votes
and multiplying the number by 100. The function accepts 2 arrays and the size of
the array and displays the desired output. Make sure to display the percentage
values to 2 digits after the decimal point.

e) Write a function that accepts the name of an output file and displays the array of
names and the array of votes to the provided output file. Each record must be
written on its own line. Note that the name of the file is entered in the main
function and the value passed to the function along with the arrays to be
displayed.

f)Write a function to return the name of the winner. DO NOT use a pre-defined
Python function. The function accepts the array of names and votes and the
number of candidates , finds the maximum number of votes and returns the name
associated with the maximum number of votes.

g)Write a function to return the name of the loser. DO NOT use a pre-defined
Python function. The function accepts the array of names and votes and the
number of candidates, finds the minimum number of votes and returns the name
associated with the minimum number of votes.

h) Write a function to sort the candidates by name. I just need the names in a sorted
order. The function accepts an array of names and sorts the data. After the
function is called, the data would be in alphabetical order.

asked
User JVC
by
8.2k points

1 Answer

6 votes

Answer & Explanation:

Python code:

function to validate number of candidates

def validate_num_candidates(): while True: try: num_candidates = int(input("Enter the number of candidates: ")) if num_candidates <= 0: print("Please enter a valid number greater than 0.") continue break except ValueError: print("Please enter a valid whole number.") return num_candidates

function to get candidate names and votes

def get_candidate_data(num_candidates): candidate_names = [] candidate_votes = [] for i in range(num_candidates): name = input(f"Enter the last name of candidate {i+1}: ") while True: try: votes = int(input(f"Enter the number of votes for {name}: ")) if votes < 0: print("Please enter a valid number greater than or equal to 0.") continue break except ValueError: print("Please enter a valid whole number.") candidate_names.append(name) candidate_votes.append(votes) return candidate_names, candidate_votes

function to display candidate data

def display_candidate_data(candidate_names, candidate_votes): total_votes = sum(candidate_votes) for i in range(len(candidate_names)): percentage = round((candidate_votes[i]/total_votes)*100, 2) print(f"{candidate_names[i]} received {candidate_votes[i]} votes, which is {percentage}% of the total votes.")

function to write data to output file

def write_to_file(filename, candidate_names, candidate_votes): with open(filename, "w") as f: for i in range(len(candidate_names)): f.write(f"{candidate_names[i]}: {candidate_votes[i]}\\")

function to find winner

def find_winner(candidate_names, candidate_votes): max_votes = 0 winner = "" for i in range(len(candidate_votes)): if candidate_votes[i] > max_votes: max_votes = candidate_votes[i] winner = candidate_names[i] return winner

function to find loser

def find_loser(candidate_names, candidate_votes): min_votes = candidate_votes[0] loser = candidate_names[0] for i in range(len(candidate_votes)): if candidate_votes[i] < min_votes: min_votes = candidate_votes[i] loser = candidate_names[i] return loser

function to sort candidate names

def sort_names(candidate_names): candidate_names.sort() return candidate_names

Main program

if name == "main": # validate number of candidates num_candidates = validate_num_candidates() # get candidate data candidate_names, candidate_votes = get_candidate_data(num_candidates) # display data display_candidate_data(candidate_names, candidate_votes) # write to file filename = "output.txt" write_to_file(filename, candidate_names, candidate_votes) # find winner winner = find_winner(candidate_names, candidate_votes) print(f"The winner is: {winner}") # find loser loser = find_loser(candidate_names, candidate_votes) print(f"The loser is: {loser}") # sort names sorted_names = sort_names(candidate_names) print(f"The sorted names are: {sorted_names}")

Pseudocode

function to validate number of candidates

loop until whole number input is given

try to convert input to integer

if input is less than or equal to 0

print "Please enter a valid number greater than 0."

continue loop

break loop

except ValueError

print "Please enter a valid whole number."

return number of candidates

function to get candidate names and votes

create empty arrays for candidate names and votes

loop through number of candidates

ask user for last name of candidate

loop until valid number of votes is given

try to convert input to integer

if input is less than 0

print "Please enter a valid number greater than or equal to 0."

continue loop

break loop

except ValueError

print "Please enter a valid whole number."

append name and votes to respective arrays

return arrays of names and votes

function to display candidate data

calculate total number of votes

loop through candidates

calculate percentage of total votes received by candidate

display candidate name, votes received and percentage of total votes

function to write data to output file

open file with given filename in write mode

loop through candidates

write candidate name and votes received to file

close file

function to find winner

set maximum number of votes to 0

loop through candidates

if candidate votes is greater than maximum number of votes

set maximum number of votes to candidate votes

set winner to candidate name

return winner

function to find loser

set minimum number of votes to votes received by first candidate

loop through candidates

if candidate votes is less than minimum number of votes

set minimum number of votes to candidate votes

set loser to candidate name

return loser

function to sort candidate names

sort array of candidate names alphabetically

return sorted array of names

answered
User Aref Aslani
by
7.7k points