我曾尝试编写一个程序,可以模拟一次二十一点游戏x次。它有效,但只有当x = 1 ...
时这是我的程序代码:
import random
def deck():
deck_cards = []
card = ["Ace of spades", 11]
deck_cards.append(card)
card = ["2 of spades", 2]
deck_cards.append(card)
card = ["3 of spades", 3]
deck_cards.append(card)
card = ["4 of spades", 4]
deck_cards.append(card)
card = ["5 of spades", 5]
deck_cards.append(card)
card = ["6 of spades", 6]
deck_cards.append(card)
card = ["7 of spades", 7]
deck_cards.append(card)
card = ["9 of spades", 8]
deck_cards.append(card)
card = ["9 of spades", 9]
deck_cards.append(card)
card = ["10 of spades", 10]
deck_cards.append(card)
card = ["Jack of spades", 10]
deck_cards.append(card)
card = ["Queen of spades", 10]
deck_cards.append(card)
card = ["King of spades", 10]
deck_cards.append(card)
card = ["Ace of hearts", 11]
deck_cards.append(card)
card = ["2 of hearts", 2]
deck_cards.append(card)
card = ["3 of hearts", 3]
deck_cards.append(card)
card = ["4 of hearts", 4]
deck_cards.append(card)
card = ["5 of hearts", 5]
deck_cards.append(card)
card = ["6 of hearts", 6]
deck_cards.append(card)
card = ["7 of hearts", 7]
deck_cards.append(card)
card = ["9 of hearts", 8]
deck_cards.append(card)
card = ["9 of hearts", 9]
deck_cards.append(card)
card = ["10 of hearts", 10]
deck_cards.append(card)
card = ["Jack of hearts", 10]
deck_cards.append(card)
card = ["Queen of hearts", 10]
deck_cards.append(card)
card = ["King of hearts", 10]
deck_cards.append(card)
card = ["Ace of clubs", 11]
deck_cards.append(card)
card = ["2 of clubs", 2]
deck_cards.append(card)
card = ["3 of clubs", 3]
deck_cards.append(card)
card = ["4 of clubs", 4]
deck_cards.append(card)
card = ["5 of clubs", 5]
deck_cards.append(card)
card = ["6 of clubs", 6]
deck_cards.append(card)
card = ["7 of clubs", 7]
deck_cards.append(card)
card = ["9 of clubs", 8]
deck_cards.append(card)
card = ["9 of clubs", 9]
deck_cards.append(card)
card = ["10 of clubs", 10]
deck_cards.append(card)
card = ["Jack of clubs", 10]
deck_cards.append(card)
card = ["Queen of clubs", 10]
deck_cards.append(card)
card = ["King of clubs", 10]
deck_cards.append(card)
card = ["Ace of diamonds", 11]
deck_cards.append(card)
card = ["2 of diamonds", 2]
deck_cards.append(card)
card = ["3 of diamonds", 3]
deck_cards.append(card)
card = ["4 of diamonds", 4]
deck_cards.append(card)
card = ["5 of diamonds", 5]
deck_cards.append(card)
card = ["6 of diamonds", 6]
deck_cards.append(card)
card = ["7 of diamonds", 7]
deck_cards.append(card)
card = ["9 of diamonds", 8]
deck_cards.append(card)
card = ["9 of diamonds", 9]
deck_cards.append(card)
card = ["10 of diamonds", 10]
deck_cards.append(card)
card = ["Jack of diamonds", 10]
deck_cards.append(card)
card = ["Queen of diamonds", 10]
deck_cards.append(card)
card = ["King of diamonds", 10]
deck_cards.append(card)
return deck_cards
def first_deal(initial_deck):
new_deck0 = initial_deck
new_dealer0 = 0
new_hand0 = 0
for i in range(2):
card_h = random.choice(new_deck0)
new_hand0 += card_h[1]
new_deck0.remove(card_h)
card_d = random.choice(new_deck0)
new_dealer0 += card_d[1]
new_deck0.remove(card_d)
return new_deck0, new_hand0, new_dealer0
def bot_turn(new_deck1, new_hand1, new_dealer1):
new_deck2 = new_deck1
new_hand2 = new_hand1
new_dealer2 = new_dealer1
while new_hand1 <= 17:
card_d = random.choice(new_deck2)
new_hand2 += card_d[1]
new_deck2.remove(card_d)
return new_deck2, new_hand2, new_dealer2
def dealer_turn(new_deck2, new_hand2, new_dealer2):
new_deck3 = new_deck2
new_hand3 = new_hand2
new_dealer3 = new_dealer2
while new_dealer3 <= 16:
card_d = random.choice(new_deck3)
new_dealer3 += card_d[1]
new_deck3.remove(card_d)
return new_deck3, new_hand3, new_dealer3
def main():
player_wins = 0
dealer_wins = 0
plays = 0
for i in range(2):
initial_deck = deck()
new_deck1, new_hand1, new_dealer1 = first_deal(initial_deck)
if new_hand1 == 21:
player_wins += 1
plays += 1
continue
elif new_hand1 > 21:
dealer_wins += 1
plays += 1
continue
elif new_dealer1 > 21:
player_wins += 1
plays += 1
continue
elif new_hand1 and new_dealer1 > 21:
plays += 1
continue
new_deck2, new_hand2, new_dealer2 = bot_turn(new_deck1, new_hand1, new_dealer1)
if new_hand2 == 21:
player_wins += 1
plays += 1
continue
elif new_hand2 > 21:
dealer_wins += 1
plays += 1
continue
new_deck3, new_hand3, new_dealer3 = dealer_turn(new_deck2, new_hand2, new_dealer2)
if new_dealer3 > 21:
player_wins += 1
plays += 1
continue
print()
print("You ran the simulation", plays, "times")
print()
print("You won", player_wins, "times")
print()
print("The dealer won", dealer_wins, "times")
print()
if __name__ == "__main__":
main()
正如您所看到的,该计划建立在三个功能上:第一笔交易,您的(机器人)转弯和经销商转向。每转一圈,程序都会检查是否有人被击败或获胜。 我遇到的问题如下:
Traceback (most recent call last):
File "C:\Users\ab50278\Portable Python\App\lib\random.py", line 249, in choice
i = self._randbelow(len(seq))
File "C:\Users\ab50278\Portable Python\App\lib\random.py", line 225, in _randbelow
r = getrandbits(k) # 0 <= r < 2**k
ValueError: number of bits must be greater than zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/ab50278/Desktop/Python/Gy-arbete/Blackjack Sim.py", line 231, in <module>
main()
File "C:/Users/ab50278/Desktop/Python/Gy-arbete/Blackjack Sim.py", line 200, in main
new_deck2, new_hand2, new_dealer2 = bot_turn(new_deck1, new_hand1, new_dealer1)
File "C:/Users/ab50278/Desktop/Python/Gy-arbete/Blackjack Sim.py", line 143, in bot_turn
card_d = random.choice(new_deck2)
File "C:\Users\ab50278\Portable Python\App\lib\random.py", line 251, in choice
raise IndexError('Cannot choose from an empty sequence')
IndexError: Cannot choose from an empty sequence
程序随机选择卡片的方式似乎有问题。但我不确切地知道问题是由什么造成的。
我不能为我的生活弄清楚为什么它不能运行模拟多次(以及stackoverflow推荐的所有其他问题,可能有我的问题的答案要么不是python或有点太复杂了)。
希望我能够很好地解释我的问题,但如果您需要查看我的其余代码,我会相应地编辑我的帖子。我真的需要这方面的帮助,因为这是我做过的第一个中等复杂的编码,我完全失去了。谢谢你回答!
答案 0 :(得分:0)
抛出此错误是因为您以某种方式向random.choices提供空列表。尝试:
random.choice([])
你会看到同样的错误。
编辑 - 编辑问题后
错误确实在提供给random.choice的空列表中。它发生在这里:
def bot_turn(new_deck1, new_hand1, new_dealer1):
new_deck2 = new_deck1
new_hand2 = new_hand1
new_dealer2 = new_dealer1
while new_hand1 <= 17:
card_d = random.choice(new_deck2)
new_hand2 += card_d[1]
new_deck2.remove(card_d)
你有错字,你最终进入无限循环 - 将while行更改为:
while new_hand2 <= 17:
现在您还可以运行主循环两次。
无论如何,你的案例是调试的好例子。我建议你使用像PyCharm这样的IDE,学习如何调试你的代码(很简单:):