代码之家  ›  专栏  ›  技术社区  ›  hazra

附加引用函数内列表的列表

  •  0
  • hazra  · 技术社区  · 10 年前

    我的任务是用Python制作一个21点游戏,最多可以让4个人类玩家玩,外加自动屋。

    我必须使用函数“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 回复  |  直到 10 年前
        1
  •  0
  •   TheSoundDefense    10 年前

    这个 get_deck 函数已经为您随机洗牌了。您不需要从其列表中获取随机元素,因为它已经是随机的。只需从迭代器中获取下一个元素 获取_确认 返回。

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