Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ValueError:位数必须大于零(python)_Python_Function_Loops_Random_Simulation - Fatal编程技术网

ValueError:位数必须大于零(python)

ValueError:位数必须大于零(python),python,function,loops,random,simulation,Python,Function,Loops,Random,Simulation,我曾试图编写一个程序,将模拟一个游戏的21点为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]

我曾试图编写一个程序,将模拟一个游戏的21点为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()
随机导入
def deck():
卡片组=[]
牌=[“黑桃王牌”,11]
卡片组。附加(卡片)
牌=[“黑桃2”,2]
卡片组。附加(卡片)
牌=[“黑桃3”,3]
卡片组。附加(卡片)
牌=[“黑桃4”,4]
卡片组。附加(卡片)
牌=[“黑桃5”,5]
卡片组。附加(卡片)
牌=[“黑桃6”,6]
卡片组。附加(卡片)
牌=[“黑桃7”,7]
卡片组。附加(卡片)
牌=[“黑桃9”,8]
卡片组。附加(卡片)
牌=[“黑桃9”,9]
卡片组。附加(卡片)
牌=[“黑桃10”,10]
卡片组。附加(卡片)
卡片=[“黑桃杰克”,10]
卡片组。附加(卡片)
卡片=[“黑桃皇后”,10]
卡片组。附加(卡片)
卡片=[“黑桃之王”,10]
卡片组。附加(卡片)
卡片=[“红桃王牌”,11]
卡片组。附加(卡片)
卡片=[“两颗心”,2]
卡片组。附加(卡片)
卡片=[“三颗心”,3]
卡片组。附加(卡片)
卡片=[“四颗心”,4]
卡片组。附加(卡片)
卡片=[“五颗心”,5]
卡片组。附加(卡片)
卡片=[“6颗心”,6]
卡片组。附加(卡片)
卡片=[“七颗心”,7]
卡片组。附加(卡片)
卡片=[“心的9”,8]
卡片组。附加(卡片)
卡片=[“心的9”,9]
卡片组。附加(卡片)
卡片=[“十颗心”,10]
卡片组。附加(卡片)
卡片=[“红心杰克”,10]
卡片组。附加(卡片)
卡片=[“红心皇后”,10]
卡片组。附加(卡片)
卡片=[“红心之王”,10]
卡片组。附加(卡片)
卡片=[“俱乐部王牌”,11]
卡片组。附加(卡片)
卡片=[“2个俱乐部”,2]
卡片组。附加(卡片)
卡片=[“3个俱乐部”,3]
卡片组。附加(卡片)
卡片=[“4个俱乐部”,4]
卡片组。附加(卡片)
卡片=[“5个俱乐部”,5]
卡片组。附加(卡片)
卡片=[“6个俱乐部”,6]
卡片组。附加(卡片)
卡片=[“7个俱乐部”,7]
卡片组。附加(卡片)
卡片=[“9个俱乐部”,8]
卡片组。附加(卡片)
卡片=[“9个俱乐部”,9]
卡片组。附加(卡片)
卡片=[“10个俱乐部”,10]
卡片组。附加(卡片)
卡片=[“俱乐部杰克”,第10页]
卡片组。附加(卡片)
卡片=[“俱乐部女王”,第10页]
卡片组。附加(卡片)
卡片=[“俱乐部之王”,10]
卡片组。附加(卡片)
卡片=[“钻石王牌”,11]
卡片组。附加(卡片)
卡片=[“钻石之二”,2]
卡片组。附加(卡片)
卡片=[“钻石之三”,3]
卡片组。附加(卡片)
卡片=[“四颗钻石”,4]
卡片组。附加(卡片)
卡片=[“5颗钻石”,5]
卡片组。附加(卡片)
卡片=[“6颗钻石”,6]
卡片组。附加(卡片)
卡片=[“7颗钻石”,7]
卡片组。附加(卡片)
卡片=[“9颗钻石”,8]
卡片组。附加(卡片)
卡片=[“9颗钻石”,9]
卡片组。附加(卡片)
卡片=[“10颗钻石”,10]
卡片组。附加(卡片)
卡片=[“钻石杰克”,10]
卡片组。附加(卡片)
卡片=[“钻石女王”,10]
卡片组。附加(卡片)
卡片=[“钻石之王”,10]
卡片组。附加(卡片)
回程卡
def首次交易(初始组):
新甲板0=初始甲板
新经销商0=0
new_hand0=0
对于范围(2)中的i:
卡片h=随机选择(新的0)
new_hand0+=卡牌[1]
新卡0.卸下(卡h)
卡片d=随机选择(新的0)
新经销商0+=卡d[1]
新卡0.卸下(卡d)
返回新的\u deck0、新的\u hand0、新的\u dealer0
def bot_turn(新甲板1、新把手1、新经销商1):
新甲板2=新甲板1
新手2=新手1
新经销商2=新经销商1
而新(1 21):
玩家赢+=1
播放次数+=1
持续
elif new_hand1和new_dealer1>21:
播放次数+=1
持续
新甲板2、新甲板2、新甲板2=自动转弯(新甲板1、新甲板1、新甲板1)
如果new_hand2==21:
玩家赢+=1
播放次数+=1
持续
elif new_hand2>21:
庄家赢+=1
播放次数+=1
持续
新甲板3、新甲板3、新甲板3=新甲板2、新甲板2、新甲板2、新甲板2)
如果新经销商3>21:
玩家赢+=1
播放次数+=1
持续
打印()
打印(“您运行了模拟”,播放,“时代”)
打印()
打印(“你赢了”,玩家赢了,“时代”)
打印()
打印(“经销商赢了”,经销商赢了,“时代”)
打印()
如果名称=“\uuuuu main\uuuuuuuu”:
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
回溯(最近一次呼叫最后一次):
文件“C:\Users\ab50278\Portable Python\App\lib\random.py”,第249行,在选项中
i=自身.(列(序号))
文件“C:\Users\ab50278\Portable Python\App\lib\random.py”,第225行,在下面的

r=getrandbits(k)#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)
def bot_turn(新甲板1、新把手1、新经销商1):
新甲板2=新甲板1
新手2=新手1
新经销商2=新经销商1

虽然是新手,但我发现还有一些缺陷。我建议您将整个代码发布到某个地方。尝试以下方法来生成您的牌组:
suits=['diamonds','hearts','spades','clubs']
cards='Ace 2 3 4 5 6 7 8 9 10
while new_hand2 <= 17: