Python 如何使函数动态运行两次

Python 如何使函数动态运行两次,python,list,function,variables,dynamic,Python,List,Function,Variables,Dynamic,这是一个函数 def cut_dk(): a = []; b = [] random.shuffle(deck__) slice = deck__[:2] a.append(slice[0]) b.append(slice[1]) return a,b c = cut_dk() p1 = c[0] p2 = c[1] 我在程序顶部有这个函数和其他函数。 它从预定义的列表中绘制。 在程序中动态调用此函数时,它返回相同的变量。 这是对一副牌组的切割

这是一个函数

def cut_dk():
    a = []; b = []
    random.shuffle(deck__)
    slice = deck__[:2]
    a.append(slice[0])
    b.append(slice[1])
    return a,b

c = cut_dk()
p1 = c[0]
p2 = c[1]
我在程序顶部有这个函数和其他函数。 它从预定义的列表中绘制。 在程序中动态调用此函数时,它返回相同的变量。 这是对一副牌组的切割(两张牌,最高的赢得抽签),当牌相等时,它需要再次抽签(这是问题,第二次抽签),从列表中选择一个新的选项,但它只是重复它在内存中的变量


在条件语句中再次调用该函数只会返回第一次运行时获得的相同初始变量,因此我无法在游戏中动态重复剪切。

我将使用
类作为
对象管理我的牌组。这将允许我们定义一个deck,它存储为一个对象,并对deck执行多个功能,同时保留不同功能的状态更改

class deck:
    """
        Class designed to manage deck including iterations of changes. 
        Shuffling the deck will retain shuffle changes 
    """
    def __init__(self):
        self.deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'J', 'Q', 'K', 'A']
        self.original = self.deck[:]

    def shuffle(self):
        """
           Shuffle deck in-place. self.deck will be modified
        """
        random.shuffle(self.deck)

    def cut(self):
        """
           Shuffles deck and draws the two top-most cards
           
           Returns: tuple(2) two cards from top of the deck
        """
        self.shuffle()
        a, b = self.deck[:2]
        return a, b

    def highest_draw(self):
        """
           Calls self.cut() to shuffle and retrieve 2x top-most cards. 

           If cards match the deck is shuffled and cut again.

           Returns: 2 Unique cards from top of the deck
        """
        a, b = self.cut()
        while a == b:
            a, b = self.cut()
        return a, b

    def reset(self):
        self.deck = self.original[:]


您仍然需要定义如何确定“最高”牌,但这取决于您的牌组,您没有考虑到这一点。

我将使用
类作为
对象管理我的牌组。这将允许我们定义一个deck,它存储为一个对象,并对deck执行多个功能,同时保留不同功能的状态更改

class deck:
    """
        Class designed to manage deck including iterations of changes. 
        Shuffling the deck will retain shuffle changes 
    """
    def __init__(self):
        self.deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'J', 'Q', 'K', 'A']
        self.original = self.deck[:]

    def shuffle(self):
        """
           Shuffle deck in-place. self.deck will be modified
        """
        random.shuffle(self.deck)

    def cut(self):
        """
           Shuffles deck and draws the two top-most cards
           
           Returns: tuple(2) two cards from top of the deck
        """
        self.shuffle()
        a, b = self.deck[:2]
        return a, b

    def highest_draw(self):
        """
           Calls self.cut() to shuffle and retrieve 2x top-most cards. 

           If cards match the deck is shuffled and cut again.

           Returns: 2 Unique cards from top of the deck
        """
        a, b = self.cut()
        while a == b:
            a, b = self.cut()
        return a, b

    def reset(self):
        self.deck = self.original[:]


您仍然需要定义如何确定“最高”牌,但这取决于您的牌组,您已将其排除在问题之外。

听起来好像生成器函数在这里很有用。调用函数一次以设置迭代器,然后从迭代器中“绘制”卡片(在本例中,迭代器为“卡片”)。注意,你必须抓住这样一种情况:你跑了整个甲板,它被捆绑在一起。我在本文中添加了一些打印语句,以便更容易理解生成器的工作原理

import random 

deck__ = list(range(3))*2

def draw_from_shuffled():
    random.shuffle(deck__)
    print(f'Deck after shuffling: {deck__}')
    for c in deck__:
        yield c

        
cards = draw_from_shuffled() #cards is now an iterator   
while True:
    try:
        a = next(cards)
        b = next(cards)
    except StopIteration:
        print(f'End of deck!')
        cards = draw_from_shuffled()
        continue
    print(f'Drew {a} and {b}')
    if a != b:
        break
print('Hand done.')
样本输出:

Deck after shuffling: [2, 2, 1, 1, 0, 0]
Drew 2 and 2
Drew 1 and 1
Drew 0 and 0
End of deck!
Deck after shuffling: [0, 0, 2, 2, 1, 1]
Drew 0 and 0
Drew 2 and 2
Drew 1 and 1
End of deck!
Deck after shuffling: [0, 2, 1, 0, 1, 2]
Drew 0 and 2
Hand done.

关于生成器的更多信息:

听起来生成器函数在这里很有用。调用函数一次以设置迭代器,然后从迭代器中“绘制”卡片(在本例中,迭代器为“卡片”)。注意,你必须抓住这样一种情况:你跑了整个甲板,它被捆绑在一起。我在本文中添加了一些打印语句,以便更容易理解生成器的工作原理

import random 

deck__ = list(range(3))*2

def draw_from_shuffled():
    random.shuffle(deck__)
    print(f'Deck after shuffling: {deck__}')
    for c in deck__:
        yield c

        
cards = draw_from_shuffled() #cards is now an iterator   
while True:
    try:
        a = next(cards)
        b = next(cards)
    except StopIteration:
        print(f'End of deck!')
        cards = draw_from_shuffled()
        continue
    print(f'Drew {a} and {b}')
    if a != b:
        break
print('Hand done.')
样本输出:

Deck after shuffling: [2, 2, 1, 1, 0, 0]
Drew 2 and 2
Drew 1 and 1
Drew 0 and 0
End of deck!
Deck after shuffling: [0, 0, 2, 2, 1, 1]
Drew 0 and 0
Drew 2 and 2
Drew 1 and 1
End of deck!
Deck after shuffling: [0, 2, 1, 0, 1, 2]
Drew 0 and 2
Hand done.

关于生成器的更多信息:

这是我的牌组[[1,10,“|-9-|',“|-Hearts-|',”=Red='](除了它有24张牌(24张列表),9张以上),一张牌包含一个切割牌组的分数,一个游戏分数,牌的实际等级,牌组和牌的颜色。牌组的得分取决于交易结束后出现的一张牌(euchre,割喉,每个人都为自己),实际上我遇到的主要问题是切牌,其余的只是一个纸牌游戏,一张牌接着一张牌,直到手结束。。。谢谢你的评论,这很有帮助,我很高兴我没有选择“克里贝奇”。这是我的牌组[[1,10,'.-9-'.','.-Hearts-'.','=Red=']](除了它有24张牌(24张名单),9张以上),一张牌包含一个切牌得分,一个游戏得分,牌的实际等级,牌组和牌的颜色。牌组的得分取决于交易结束后出现的一张牌(euchre,割喉,每个人都为自己),实际上我遇到的主要问题是切牌,其余的只是一个纸牌游戏,一张牌接着一张牌,直到手结束。。。谢谢你的评论,这很有帮助,我很高兴我没有选择“克里贝奇”。谢谢,很有帮助,实际问题很简单,我可以用掷硬币来轻松解决它,但在游戏中,我一直使用甲板切割法来确定发牌人,这也成为了一个难题,我确信这是我的编码需要更多的关注。谢谢,非常有用,实际问题非常简单,我可以通过掷硬币轻松解决它,但在游戏中,我一直使用甲板切割方法来确定发牌人,这也成为了一个难题,我确信我需要更多关注的是编码。