Python 检查是否可以在另一个列表中找到两个列表成员的总和

Python 检查是否可以在另一个列表中找到两个列表成员的总和,python,Python,在我写的这个程序中,到目前为止,你得到了一组数字。您可以选择要玩的骰子数量。每回合你掷骰子的数量,并选择其中两个骰子和你原来的数字集中的一个数字 我试图做到这一点,如果没有可能的数字求和,就可以从原始数字集中删除一个数字。它会说“对不起,没有可能的解决办法”,并跳过该回合 最终,我会把电脑添加到vs中,这也是它将跳过的内容,但我想先让它工作 import random lst_player = [2,3,4,5,6,7,8,9,10,11,12] lst_cpu = [2,3,4,5,6,7,8

在我写的这个程序中,到目前为止,你得到了一组数字。您可以选择要玩的骰子数量。每回合你掷骰子的数量,并选择其中两个骰子和你原来的数字集中的一个数字

我试图做到这一点,如果没有可能的数字求和,就可以从原始数字集中删除一个数字。它会说“对不起,没有可能的解决办法”,并跳过该回合

最终,我会把电脑添加到vs中,这也是它将跳过的内容,但我想先让它工作

import random
lst_player = [2,3,4,5,6,7,8,9,10,11,12]
lst_cpu = [2,3,4,5,6,7,8,9,10,11,12]

num_dice = int(input("Enter the number of dice you would like to play with 3-5 : 
"))

def stars():
    count = 0
    for count in range(30):
        count += 1
        print("*", end=' ')
    print("")

stars()

print("Player set","    ",lst_player)
print("Computer set","  ",lst_cpu)

stars()

while lst_player or lst_cpu != []:

    print("")

    roll = input("Player turn, enter 'e' to roll : ")
    dice_lst = []
    if roll == "e":
        for e in range(num_dice):
            d_lst = random.randint(1,6)
            dice_lst.append(d_lst)
    else:
        print("Invalid input")
        continue 

    while True:

        print("")
        print("You rolled", dice_lst)
        dice_choose = int(input("Choose a dice : "))
        dice_lst.remove(dice_choose)
        print("")
        print("You are left with", dice_lst)
        dice_choose1 = int(input("Choose a dice : "))
        dice_lst.remove(dice_choose1)
        print("")

        sum1 = dice_choose + dice_choose1
        if sum1 not in lst_player:
            dice_lst.append(dice_choose)
            dice_lst.append(dice_choose1)
            print("You have made an invalid choice")
            continue

        print(dice_choose, "+", dice_choose1, "sums to", sum1)
        print(sum1, "will be removed")
        print("")

        lst_player.remove(sum1)
        break

    stars()

    print("Player set","    ",lst_player)
    print("Computer set","  ",lst_cpu)

    stars()

else:
    print("You have won or lost. IDK yet.")
我的问题: 我试图做到这一点,如果没有可能的数字求和,就可以从原始数字集中删除一个数字。它会说“对不起,没有可能的解决办法”,并跳过该回合

例如:

dice_lst = [1, 2, 3]
lst_player = [6, 7, 8]

由于无论是
1+2=>3
还是
1+3=>4
还是
2+3=>5
都无法在
lst\U player
中找到,我想打印
“对不起,没有可能的解决方案”
如果我理解清楚,这就是您要找的:

这不是最优雅的解决方案,而是一个相当容易理解的解决方案 并且不需要任何其他python模块的知识

您只需尝试两个骰子的所有组合,计算总和,如果可以找到一个总和,则返回
True
(并停止寻找其他解决方案) 否则(如果您尝试了所有组合,但未发现任何结果) 返回
False

现在,您可以在代码中使用此函数来检查是否要询问用户选择哪个骰子,或者是否立即
继续
循环

def can_remove_a_number(thrown_dice, remaining_numbers):
    """
    checks whether the sum of two dice out of thrown_dice
    can be found in remaining_numbers
    """
    for idx1 in range(len(thrown_dice) - 1):
        for idx2 in range(idx1 + 1,  len(thrown_dice)):
            # next line for debugging only
            print("trying die %d and die %d" % (idx1, idx2))
            sum_ = thrown_dice[idx1] + thrown_dice[idx2]
            # next line for debugging only
            print("sum = ", sum_)
            if sum_ in remaining_numbers:
                return True
    return False
以及测试:

print("*" * 30)
print(can_remove_a_number([1,2,2], [5,6,7]))
print("*" * 30)
print(can_remove_a_number([1,2,2], [4,5,6,7]))
print("*" * 30)

# or if you want to write a test
# assert <expression> breaks if expression is not True
assert not can_remove_a_number([1,2,2], [5,6,7])
print("*" * 30)

assert can_remove_a_number([1,2,2], [4,5,6,7])
print("*" * 30)
或者如果您听说过列表理解/理解():

或者用另一种理解

def can_remove_a_number(thrown_dice, remaining_numbers):
    for found in (sum(dice) in remaining_numbers 
                  for dice in combinations(thrown_dice, 2)):
        if found:
            return True
    return False
或者和任何函数()


你的问题是什么?最好把你的问题表达得非常明确,并把我读了好几遍的最短的问题贴出来,直到我明白你想要什么。这是你的问题吗?我想检测一种情况,即在给定列表中找不到
n
骰子列表中两个骰子的任何和。示例:
dice_lst=[1,2,3]
但是
lst_player=[6,7,8]
既不是
1+2=>3
也不是
1+3=>4
也不是
2+3=>5
可以在
lst_player
中找到,我想打印“对不起,没有可能的解决方案”是的,很抱歉,下次我会把它缩短。还有需要注意的事情:您必须验证
dice\u choose
是否有有效值,如果没有,您的脚本将中断。同样,如果一个人意外地打错了号码,脚本中止,那么对于
掷骰子和
掷骰子都是非常令人沮丧的。我会很快写下你问题的答案。也请注意。如果有人给你反馈或评论,编辑你的问题并加以修改是个好主意,这样更容易阅读/理解。事实上,写一个简单易懂的问题是相当困难的,但你会找到诀窍你的问题被否决了。为了避免进一步降低投票率,我会稍微修改一下这个问题。修复了3个打字错误,并添加了测试代码。如果您编写了一些测试,那么就更容易对函数进行middify/optimize/refactor,并查看它是否仍以同样的方式运行。我的2个小测试可能还不够,但是您会得到一步步缩短的解决方案,但是它们需要更多的python知识才能理解。我加上了林克斯利。我该怎么做?我从来不知道有一个被接受的按钮。我想是我做的。它是这么说的。谢谢你的反馈!声誉低于15的人所投的票将被记录,但不会改变公开显示的帖子分数。
def can_remove_a_number(thrown_dice, remaining_numbers):
    for sum_ in (sum(dice) for dice in combinations(thrown_dice, 2)):
        if sum_ in remaining_numbers:
            return True
    return False
def can_remove_a_number(thrown_dice, remaining_numbers):
    for found in (sum(dice) in remaining_numbers 
                  for dice in combinations(thrown_dice, 2)):
        if found:
            return True
    return False
def can_remove_a_number(thrown_dice, remaining_numbers):
    return any(sum(dice) in remaining_numbers 
               for dice in combinations(thrown_dice, 2))