附加一个列表,该列表引用函数内的列表

时间:2014-07-27 17:26:27

标签: python function python-2.7 blackjack

我的任务是用Python生成一个二十一点游戏,允许最多4个人类玩家加上自动化的House。

我必须使用函数'get_deck'(参见下面的代码)。 我正在努力弄清楚如何从'get_deck'获得一张牌并将其附加到玩家的名单上。 我设法在使用我自己定义的卡片列表时制作程序,但是对于这个作业,我必须使用'get_deck'函数。 '?????'在下面的代码中我引用了我的第一个卡值列表。

这是我的代码:

def get_deck():
 deck = [value + suit for value in '23456789TJQKA' for suit in 'SHDC']
 random.shuffle(deck)
 return iter(deck)

while True:
 get_deck()  
 player1 = []
 player2 = []
 player3 = []
 player4 = []
 house = []  
 player1.append(random.choice(?????))  
 player2.append(random.choice(?????))
 player3.append(random.choice(?????))
 player4.append(random.choice(?????))
 house.append(random.choice(?????))
 player1_bust = False
 player2_bust = False
 player3_bust = False
 player4_bust = False
 house_bust = False  
 if number_players[0] == 1:  
    player1_total = total(player1)
    while True:
        player1_total = total(player1)
        print "Player 1 has these cards %s with a total value of %d." % (player1, player1_total)
        if player1_total > 21:
            print "Bust!"
            player1_bust = True
            break
        elif player1_total == 21:
            print "Blackjack!"
            break
        else:
            hit_stand = raw_input("Hit or Stand? (h or s): ").lower()
            if 'h' in hit_stand:
                player1.append(random.choice(?????))
            else:
                break

我希望这是有道理的!提前谢谢。

使用Python 2.7

1 个答案:

答案 0 :(得分:0)

get_deck功能已经随机随机改组。您不需要从列表中获取随机元素,因为它已经是随机的。只需从get_deck返回的迭代器中获取下一个元素。

https://docs.python.org/2/library/stdtypes.html#iterator-types