[英]How to calculate scores without using enumerate
I have designed this python card game, I'm wondering if there is a better way to calculated each player scores without using the enumerate option.我设计了这个 python 纸牌游戏,我想知道是否有更好的方法来计算每个玩家的分数而不使用 enumerate 选项。 I'm very new to Coding/Python only few weeks.我才刚接触编码/Python 几周。 Only know the bare basics.My friend was helped me do this.只知道最基本的知识。我的朋友帮助我做到了这一点。
def select_winner(players_scores, player_names):
max = 0
winner = ''
print('The game scores are:')
for index, score in enumerate(players_scores):
current_player = player_names[index]
print(current_player, "=", score)
if score > max:
max = score
winner = current_player
print("The winner is:", winner)
Is there a simpler way to do this, that will help me understand it better/easily.有没有更简单的方法来做到这一点,这将帮助我更好/更容易地理解它。
Use zip if you really just don't want to use enumerate:如果您真的不想使用枚举,请使用zip :
for current_player, score in zip(player_names, player_scores):
print(current_player)
print(score)
<etc>
zip lets you loop over two lists at once in the same order. zip 允许您以相同的顺序一次循环遍历两个列表。
But yea you can also just use Python's max function to find the maximum score.但是是的,您也可以仅使用 Python 的 max 函数来查找最高分数。
An easy way to reduce the number of lines if you only want to find the best score would be to the index() and max() methods included in the standard python library.如果您只想找到最佳分数,减少行数的一种简单方法是使用标准 python 库中包含的 index() 和 max() 方法。 An edge case you have to consider is what happens if there two people with the same max score.您必须考虑的一个极端情况是,如果有两个人的最高分数相同会发生什么。
def select_winner(players_scores, player_names):
index_of_max = players_scores.index(max(players_scores))
print('The winner is:', players_names[index_of_max],
'with a score of', players_scores[index_of_max])
If you absolutely want to list all the scores too, you should use the handy zip() function that lets you access multiple iterables at once.如果您也绝对想列出所有分数,则应该使用方便的 zip() 函数,该函数可让您一次访问多个可迭代对象。 For example:例如:
list1 = (1, 2, 3)
list2 = ('a', 'b', 'c')
for a, b in zip(list1, list2):
print(a, b)
Instead of looping through all the players, you can create a dictionary with the player name as the key and their score as the value.您可以创建一个以玩家名称为键、以他们的分数为值的字典,而不是遍历所有玩家。 Because we are in the endgame now, speed probably doesn't matter too much.因为我们现在处于最后阶段,所以速度可能并不重要。
Here's the code:这是代码:
def select_winner(players_scores, player_names):
print('The game scores are:')
# This creates the dictionary of the players
players = dict(zip(player_names, player_scores))
# order based on the score
ordered = sorted(players.items(), key=lambda player: player[1])
# last one in the sorted list (largest score)
winner_name, winner_score = ordered[-1]
print(
"The winner is:", winner_name,
"with an astounding", winner_score, "points!",
)
The players
will be something like: players
将是这样的:
{
"Gee": 10,
"Vye": 40,
"Tor": 20,
"Kat": 30,
}
Getting a dictionary's .items()
returns a bunch of 2-tuples like ("Gee", 10)
.获取字典的.items()
返回一堆 2 元组,例如("Gee", 10)
。 (In reality they return an iterator but that's too advanced for now.) (实际上他们返回一个迭代器,但现在太先进了。)
The ordered
list will be like: ordered
列表将如下所示:
[
("Gee", 10),
("Tor", 20),
("Kat", 30),
("Vye", 40),
]
By taking the final tuple, ("Vye", 40)
, we get the winner's name and score.通过取最后一个元组("Vye", 40)
,我们得到获胜者的姓名和分数。 Putting winner_name, winner_score = ("Vye", 40)
is the same as winner_name = ("Vye", 40)[0]; winner_score = ("Vye", 40)[1]
把winner_name, winner_score = ("Vye", 40)
与winner_name = ("Vye", 40)[0]; winner_score = ("Vye", 40)[1]
winner_name = ("Vye", 40)[0]; winner_score = ("Vye", 40)[1]
. winner_name = ("Vye", 40)[0]; winner_score = ("Vye", 40)[1]
。
Hope you understand all of this.希望你明白这一切。 If not, just comment the question.如果没有,请评论问题。 Else, code on!否则,打开代码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.