无法分配列表理解?

时间:2017-09-12 23:42:05

标签: python list-comprehension python-2.x

我一直在编写游戏进行学校评估,所以请原谅基本代码。我的问题是我收到一个错误,说SyntaxError:不能在main.py的第151行分配列表理解。作为参考,这是函数。

[object Object]

此行出现错误。

def winner(score1,score2,score3,score4,score5):
global players
string = 'total'
string2 = 'score'
[string+str(k) for k in range(0,players)] = abs(200 - [string2+str(p) for p in range(0,players)])
king = 100000
counter = 0
for h in range (0,players):
 if total[h] < king:
   king = total[h]
   counter = counter + 1
   print ('The winner is ' + player[counter] + ' with a score of ' + str(score[counter - 1]))
   print 'Congrats!'
   print 'Want to play again?'

有人可以解释一下我做错了什么吗?

1 个答案:

答案 0 :(得分:2)

我认为你想要的是

def winner(scores):
    totals = [abs(200 - score) for score in scores]
    max_total = max(totals)
    max_index = totals.index(max_total)
    best_score = scores[max_index]
    best_player = max_index + 1
    print("The winner is player {} with a score of {}".format(best_player, best_score))

然后你可以像

一样调用
>>> winner([-300, 100, 400, -700])
The winner is player 4 with a score of -700