我試圖編寫一個練習的代碼,雖然它的工作方式應該如此,但它看起來很可怕,我想知道是否有人可以幫助我刪除任何不必要的東西,並且可能只是結合一些功能?在列表中添加項目 - Python
下面是使用功能的一個例子:
choices([['YES', 'NO', 'YES', 'YES'], ['NO', 'NO', 'YES', 'NO'], ['YES', 'YES', 'YES', 'YES']])
列表中的每個列表有四個是/否選擇(下面列出的四個選項指數爲好,如綠色,紅色,藍色,黃色;但它不一定是四)。列表中的列表數量是多少人投票。
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
INDEX_TO_NAME
只是一個字典,它將索引連接到選項(顏色)。
基本上,代碼應該將每個yes計爲1分,每個no計爲零分,將每個可用選項的總積分加起來,然後返回總計和獲勝者。
謝謝!這有幫助! – user52610