<p>此代码使用<a href="https://www.w3schools.com/python/gloss_python_function_arbitrary_arguments.asp" rel="nofollow noreferrer">Arbitrary Arguments (*args)</a>获取输入,然后使用字典存储所有数据</p>
<pre><code>def marble_game(*list_of_kids):
# store all kids' results along with their marbles
scores = {}
for kids_marbles in list_of_kids:
# reset score for everyone
points = 0
for i in kids_marbles:
if i == "black":
points += 3
elif i == "red":
points += 5
elif i == "silver":
points += 7
elif i == "white":
scores[points] = kids_marbles
# stop counting
break
# every other color scores 2
else:
points+=2
# save score
scores[points] = kids_marbles
# return the max score
max_score = max(scores.keys())
return scores[max_score], max_score
print(marble_game(["red","red","silver"]))
print(marble_game(["red", "red", "silver"], ["blue", "blue", "blue"]))
# output
(['red', 'red', 'silver'], 17)
(['red', 'red', 'silver'], 17)
</code></pre>