I was trying to write the code for an exercise, and while it works as it should, it looks terrifying and I was wondering if anyone could help me remove anything that's not necessary, and potentially just combine some of the functions?
Here's an example of the use of the function:
choices([['YES', 'NO', 'YES', 'YES'], ['NO', 'NO', 'YES', 'NO'], ['YES', 'YES', 'YES', 'YES']])
Each list within the list has four yes/no choices (INDICES below lists four options as well, e.g. green, red, blue, yellow; but it doesn't have to be four). The number of lists within the list is how many people cast their votes.
i = 0
num = 0
total_num = 0
num_choices = len(INDICES)
choices_index = 0
choices_votes = []
choices_votes_num = []
index = 0
total_choices = []
winning_choice = ''
winning_index = 0
while i < len(parameter):
while num < num_choices:
for item in parameter:
choices_votes.append(item[num])
num += 1
i += 1
while total_num < len(choices_votes):
if choices_votes[total_num] == 'YES':
choices_votes_num.append(1)
total_num += 1
elif choices_votes[total_num] == 'NO':
choices_votes_num.append(0)
total_num += 1
while choices_index < len(choices_votes_num):
count = int(len(choices_votes_num) / num_choices)
total = 0
total = sum(choices_votes_num[choices_index:(choices_index + count)])
total_choices.append(total)
choices_index = choices_index + count
for score in total_choices:
winning_index = max(total_choices)
winning_choice = INDEX_TO_NAME[total_choices.index(winning_index)]
return winning_choice, total_choices
INDEX_TO_NAME is just a dictionary set up to connect indices to choices (the colours).
Basically the code is supposed to count each yes as 1 point and each no as zero points, add up the total points for each available choice, and then return the totals and the winner.