简体   繁体   English

如何在不使用枚举的情况下计算分数

[英]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.

相关问题 如何通过在python中使用Z分数来计算相关性? - How to calculate correlation by using Z scores in python? 如何在不使用 pandas 或 numpy 等库的情况下计算二维列表中测试分数的百分比和平均值 - How to calculate percentage and average of test scores in a 2D list without using libraries like pandas or numpy 如何使用 Python (scikit-learn) 计算 FactorAnalysis 分数? - How to calculate FactorAnalysis scores using Python (scikit-learn)? 如何在不实际使用 enumerate 的情况下制作功能类似于 enumerate 的代码? - How to make code that's functionally similar to enumerate without actually using enumerate? 在不使用枚举的情况下如何获得相同的结果? - How do I get same result without using enumerate? 复制枚举函数的功能而不使用它 - Replicating the functions of the enumerate function without using it 替换列表中的所有值而不使用枚举? - Replace all values in a list without using enumerate? 如何枚举不带括号的列表? - How do I enumerate a list without parenthesis? 如何轻松计算可读性分数或如何为此编写函数? - How to calculate readibility scores easily or how can i write a function for that? Python:如何计算分数以及如何行使时间限制? - Python: How to calculate scores and how to exercise a time limit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM