简体   繁体   English

如何使用多个 or 语句?

[英]How to use multiple or statements?

I'm counting the rolls of a dice, and I want my conditions for a 3 of a kind to be that 3 of the 5 rolls are equal, but the other two are not only different from the 3, but different from each other as well.我在数骰子的掷骰数,我希望我的 3 的条件是 5 个掷骰中的 3 个是相等的,但另外两个不仅与 3 个不同,而且彼此不同,因为出色地。

def is_it_three_of_a_kind(self):
    rl = []
    for roll in self.rolls:
        rl.append(roll)
    for i in rl:
        rl.count(i)
        rl.sort()
        if rl.count(i) == 3 and rl[0] != rl[1] or rl[3] != rl[4]:
            return True

Using multiple or statements is my best guess but adding both to it makes it check that both rl[0].= rl[1] and rl[3].= rl[4] at the same time.使用多个 or 语句是我最好的猜测,但是将两者都添加到它可以同时检查 rl[0].= rl[1] 和 rl[3].= rl[4] 。 I also just realized that the 3 groupings of the same numbers can be located in the middle so using the train of thought that I was with this code I'd have to add another or statement saying that "rl[0] != rl[4].我也刚刚意识到相同数字的 3 个分组可以位于中间,因此使用我使用此代码的思路,我必须添加另一个或声明“rl[0]!= rl[ 4]。

You will tie yourself in knots trying to list all of the possibilities by referring to each of the five dice by index.您将通过索引参考五个骰子中的每一个来将自己打结,试图列出所有可能性。 Instead, use a Counter object or similar structure to count the quantity of each number in your five dice.相反,使用Counter object 或类似结构来计算五个骰子中每个数字的数量。

For instance, given a roll of 3 5 3 3 2 , your current method would need to identify例如,给定滚动3 5 3 3 2 ,您当前的方法需要识别

rl[0] == rl[2] and rl[2] == rl[4] and \
rl[1] != rl[0] and rl[5] != rl[0] and rl[1] != rl[5]

Even if you sort the values, this isn't going to be at all readable for determining the hand.即使您对值进行排序,这对于确定手牌也完全不可读。

Instead, reduce this to the counts: 0 1 1 0 3 0 : a 2, a 3, and three 5s.相反,将其减少到计数: 0 1 1 0 3 0 :一个 2、一个 3 和三个 5。 If you sort that list to get [3, 1, 1] , you'll have an easy time identifying your hands:如果您对该列表进行排序以获得[3, 1, 1] ,您将很容易识别您的手:

 [5]             five of a kind
 [4, 1]          four of a kind
 [3, 2]          full house
 [3, 1, 1]       three of a kind
 [2, 2, 1]       two pair
 [2, 1, 1, 1]    one pair
 [1, 1, 1, 1, 1] bust, or a possible straight

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM