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