我的任务是使用Python制作二十一点游戏,最多可允许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
该get_deck
功能已经在随机为您洗牌了。您无需从其列表中获取随机元素,因为它已经被随机化了。只需从get_deck
返回的迭代器中获取下一个元素。
https://docs.python.org/2/library/stdtypes.html#iterator-types
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句