简体   繁体   English

尝试从多个选项中选择 select,然后添加正确的选项 - 非常困惑

[英]Trying to select from multiple options, then add the correct option - very confused

if (dice1 == dice2) or (dice1 == dice3) or (dice2 == dice1) or (dice2 == dice3) 
or (dice3 == dice2) or (dice3 == dice1):
     
    score = # no clue what to put here

I need to find a way to do;我需要想办法去做; if any of these options are correct then the correct option will be chosen and the dice variables are added together, eg Are two or more dice equal?如果这些选项中的任何一个是正确的,那么将选择正确的选项并将骰子变量加在一起,例如两个或更多骰子是否相等? YES, then score = sum of two equal dice .是的,那么score = sum of two equal dice

Really confused, can anyone offer help?真的很迷茫,有大神帮忙吗?

It's not necessary to ask dice2 == dice1 if you know the answer to dice1 == dice2 .如果您知道 dice1 == dice2 的答案,则无需询问dice2 == dice1 dice1 == dice2 And could be a good idea to set score to 0 even if none of the dice are equal即使没有一个骰子相等,将得分设置为 0 也是一个好主意

With just 3 dice it's probably easiest and fastest to just test the 3 possible pairs.只需 3 个骰子,测试 3 个可能的对可能是最简单和最快的。

if dice1 == dice2 or dice1 == dice3:
    score = dice1 * 2
elif dice2 == dice3:
    score = dice2 * 2
else:
    score = 0

If you have more than 3 dice you need to make more decisions of which score to give if there are more than one pair of equals.如果您有超过 3 个骰子,您需要做出更多决定,如果有一对以上的相等,则给出哪个分数。 Here's a fast solution that returns the score from the highest pair:这是一个从最高对返回分数的快速解决方案:

def best_pair(dice):
    candidates = set()
    best = 0
    for die in dice:
        # don't bother if it's not better than best
        if die > best:
            # did we find an equal before?
            if die in candidates:
                # then it's the new best
                best = die
            else:
                # maybe next time...
                candidates.add(die)
    return best * 2

best_pair([1, 3, 1, 4, 3, 5, 3, 6, 1, 2, 4, 5, 1, 1, 1])
# 10

The collections.Counter.most_common() suggested in answer by @kaya is a possibility too but if the dice are (1, 1, 5, 5) the first item of most_common() will be the pair of ones. @kaya 在回答中建议的collections.Counter.most_common()也是一种可能性,但如果骰子是(1, 1, 5, 5)most_common()的第一项将是一对。 This is probably not expected if the dice is thrown at once.如果立即掷骰子,这可能是意料之外的。 A solution to this could be to find the maximum from collections.Counter.items() instead:对此的解决方案可能是从collections.Counter.items()中找到最大值:

from collections import Counter
dice = [1, 3, 1, 4, 3, 5, 3, 6, 1, 2, 4, 5, 1, 1, 1]
score = 2 * max((v for v, c in Counter(dice).items() if c>=2), default = 0)
# 10

This is probably overkill just for three dice, but here is one way to do it that doesn't require duplicating code: the collections.Counter class has a most_common method which can tell you both what value occurs most commonly, and how many times it occurs.这对于三个骰子来说可能是多余的,但这里有一种不需要重复代码的方法: collections.Counter class 有一个most_common方法,它可以告诉你什么值最常出现,以及它出现的次数发生。

from collections import Counter

def score(*dice):
    (value, count), = Counter(dice).most_common(1)
    return 2 * value if count >= 2 else 0

Examples:例子:

>>> score(6, 3, 6)
12
>>> score(4, 2, 3)
0
>>> score(5, 5, 5)
10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Selenium (Python) 中的给定选项列表中单击正确的选项 - Clicking on the correct option from a given list of options in Selenium (Python) 当没有选项时,硒如何从下拉列表中选择选项 - how selenium select the option from dropdown list when there have no options tkinter 选项菜单不会为通过.add_command() 创建的选定选项返回正确的值 - tkinter option menu doesn't return the correct value for selected options created via .add_command() 尝试选择选项 - Trying to select the option 我在选择框中遇到问题,它可以选择多个选项,所选选项在子选择框中有几个对象 - I am having a problem in select box, where it can select multiple options, the selected option has a few objects in a sub select box 如何从多个下拉菜单中选择选项Selenium - How to select options from multiple dropdown menus Selenium Options().add_experimental_option 不起作用 - Options().add_experimental_option does not work Gtk Combobox 选择多个选项 - Gtk Combobox select multiple options Select Streamlit 中复选框中的多个选项 - Select multiple options in checkboxes in Streamlit 使用硒在“选择”中添加“选项” - Add 'option' in 'select' using selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM