Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中使用check函数?_Python_Function - Fatal编程技术网

如何在python中使用check函数?

如何在python中使用check函数?,python,function,Python,Function,我目前正在完成一个围捕程序和python,正在努力确定如何“检查”一只手上是否有4张相同等级的牌(ace、2等),并将其移除。我目前的代码如下: def check(hand, score): for i in range(len(hand)- 1): j = hand[i] if j == 4: print("you have completed a set :)") for j in range(4): hand.re

我目前正在完成一个围捕程序和python,正在努力确定如何“检查”一只手上是否有4张相同等级的牌(ace、2等),并将其移除。我目前的代码如下:

def check(hand, score):

   for i in range(len(hand)- 1):

    j = hand[i]

    if j == 4:

      print("you have completed a set :)")

      for j in range(4):

        hand.remove[j]

    score += 1

  else:

    print("no sets have been found ☹")

有什么建议吗?

您可以使用
集合
在您选中的手中获得唯一的卡片,然后计算出现的次数:

def check(hand):
    score = 0
    for card in set(hand):
        count = len([x for x in hand if (x == card)])
        if count >= 4:
            score += 1
            print("you have completed a set of '{}' :)".format(card))
            for j in range(count):
                hand.remove(card)
    if score == 0:
        print("no sets have been found ☹")

    return score

h1 = [i for i in range(12)]
h2 = [i for i in (['A', 'A', 'A', 'A', 7, 7, 7, 'J'] + h1)]

print(h1)
print('Score: {}\n'.format(check(h1)))

print(h2)
print('Score: {}'.format(check(h2)))
结果:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
no sets have been found ☹
Score: 0

['A', 'A', 'A', 'A', 7, 7, 7, 'J', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
you have completed a set of '7' :)
you have completed a set of 'A' :)
Score: 2

您好@studentcoder,欢迎来到Stack Overflow。你的代码现在没有多大意义,因为缩进被弄乱了,可能是因为将代码复制到网站的问题框中的过程。你能不能让它看起来和你编辑器里的一样?由于缩进在Python中具有语法意义,因此很难按照当前的方式解释代码。此外,您能描述一下当前代码的错误吗?这是一个例外吗?如果是,请提供完整的回溯。它给出了错误的答案吗?显示示例输入和输出。请从中重复。请注意,python函数参数为。我猜你想返回
分数
。请特别注意,我们希望你在发布之前研究你的问题。这是评估power hands的一个子集,在许多编程语言中都有详细描述,包括堆栈溢出和其他方面。