我试图编写一个练习的代码,虽然它的工作方式应该如此,但它看起来很可怕,我想知道是否有人可以帮助我删除任何不必要的东西,并且可能只是结合一些功能?在列表中添加项目 - 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