Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.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
python3黑杰克索引错误_Python_Blackjack - Fatal编程技术网

python3黑杰克索引错误

python3黑杰克索引错误,python,blackjack,Python,Blackjack,我认为这是其中的一个功能,但我是一个平庸的人 看看这种东西 import random SUITS = ("\u2660", "\u2665", "\u2666", "\u2663") PIPS = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "K", "Q") deck = [] player_hand = [] def create_deck(): for suit in SUITS:

我认为这是其中的一个功能,但我是一个平庸的人 看看这种东西

import random

SUITS = ("\u2660", "\u2665", "\u2666", "\u2663")

PIPS = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "K", "Q")

deck = []

player_hand = []


def create_deck():
    for suit in SUITS:
        for pip in PIPS:
            card = (pip + suit)
            deck.append(card)
        print()
此外,我不确定我是否正确计算A

def deal_card():
    card = random.choice(deck)
    deck.remove(card)
    return card


def create_hand(hand):
    for i in range(2):
        deal = deal_card()
        player_hand.append(deal)



def print_hand(hand):
    for pip, suit in hand:
        print(pip + suit,end=" ")
    print()
def sum_hand(手):
总数=0
对于pip,玩家手上的套装:
如果pip==“J”或pip==“K”或pip==“Q”:
总数+=10
elif pip==“A”:
总数+=0
其他:
总计+=整数(pip)
对于pip,玩家手上的套装:
如果pip==“A”:
总数+=11
elif总计>21:
总数+=1
其他:
总计+=整数(pip)
返回总数
def玩家_命中(手):
总计=总手数(玩家手数)
choice==输入(“您想打还是站(h/s)?”)
而choice.lower==“h”:
如果总数<21:
玩家手牌。追加(发牌()
打印手(玩家手)
再次播放=正确
再次播放时:
甲板=[]
玩家手=[]
总计=总手数(玩家手数)
创建手牌(玩家手牌)
打印手(玩家手)
如果总数<21:
玩家击球(玩家手)
elif总计==21:
打印(“赢家!”)
其他:
打印(“半身像!”)
再次=输入(“您想再次播放(y/n)吗?”)
如果再次出现。下限==“y”:
再玩一次
其他:
再次播放=错误
输入(“\n按enter键退出”)
以下是我得到的所有错误

我不断地犯这些错误

def sum_hand(hand):
    total = 0
    for pip,suit in player_hand:
        if pip == "J" or pip == "K" or pip == "Q":
            total += 10
        elif pip == "A":
            total += 0
        else:
            total += int(pip)
    for pip,suit in player_hand:
        if pip == "A":
            total += 11
        elif total > 21:
            total += 1
        else:
            total += int(pip)
    return total


def player_hit(hand):
    total = sum_hand(player_hand)
    choice == input("would you like to hit or stand(h/s)? ")
    while choice.lower == "h":
        if total < 21:
            player_hand.append(deal_card())
            print_hand(player_hand)


play_again = True

while play_again:

    deck = []

    player_hand = []

    total = sum_hand(player_hand)

    create_hand(player_hand)

    print_hand(player_hand)

    if total < 21:
        player_hit(player_hand)
    elif total == 21:
        print("Winner!")
    else:
        print("bust!")

    again = input("Would you like to play again(y/n)?")
    if again.lower == "y":
        play_again
    else:
        play_again = False

input("\nPress enter to exit")
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/random.py”,第255行,在选项中
i=自身.(列(序号))
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/random.py”,第232行,如下所示
r=getrandbits(k)#0在您调用
deal_card()
时,看起来卡片组是空的(即仍然等于[])。如果在调用
deal\u card()
之前调用
create\u deck()
,您应该不会有问题。

错误(回溯)是自解释的:
索引器:无法从空序列中进行选择
。从您的代码(至少,您共享的内容):
card=random.choice(deck)
deck=[]
。可能您应该在初始化
deck
后调用
create\u deck()
以清空列表。
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/random.py", line 255, in choice
    i = self._randbelow(len(seq))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/random.py", line 232, 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 "/Users/sicarius/Desktop/Intro to programming/program9.py", line 81, in <module>
    create_hand(player_hand)
  File "/Users/sicarius/Desktop/Intro to programming/program9.py", line 33, in create_hand
    deal = deal_card()
  File "/Users/sicarius/Desktop/Intro to programming/program9.py", line 26, in deal_card
    card = random.choice(deck)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/random.py", line 257, in choice
    raise IndexError('Cannot choose from an empty sequence')
IndexError: Cannot choose from an empty sequence