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