【问题标题】:ValueError: number of bits must be greater than zero (python)ValueError:位数必须大于零(python)
【发布时间】:2016-03-16 01:57:25
【问题描述】:

我试图编写一个程序来模拟二十一点游戏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 中,要么有点太复杂了)。

希望我已经很好地解释了我的问题,但如果您需要查看我的其余代码,我会相应地编辑我的帖子。在这件事上我真的需要帮助,因为这是我做过的第一个中等复杂的编码,我完全不知所措。谢谢你的回答!

【问题讨论】:

  • 我可以看到还有一些缺陷。我建议您将整个代码发布到某个地方。
  • 尝试这样的方法来生成你的牌组:suits = ['diamonds', 'hearts', 'spades', 'clubs']cards = 'Ace 2 3 4 5 6 7 8 9 10 Jack Queen King'.split()deck = ['%s of %s' % (card, suit) for card in cards for suit in suits]。想想我做了什么并记住:每当你的代码看起来重复时,都有更好的方法来做。
  • 是的,这样可以节省一些空间。但是创建套牌的方式真的可以让我免于出错吗?

标签: python function loops random simulation


【解决方案1】:

抛出此错误是因为您以某种方式向 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 并学习如何调试你的代码(很简单:)

【讨论】:

  • 我会尝试格式化代码。同时,你知道问题的解决方法吗?
  • 格式现在修复了吗?我尝试缩进几行。
猜你喜欢
  • 1970-01-01
  • 2017-04-08
  • 2017-01-30
  • 2018-05-24
  • 2021-12-10
  • 2018-03-30
  • 2020-04-01
  • 2017-08-17
  • 2016-07-02
相关资源
最近更新 更多