Python3黑杰克指数

2024-04-19 16:47:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

我认为这两种功能都有,但我是个平庸的人 在这种事情上

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(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")

以下是我得到的所有错误

我不断地犯这些错误

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

非常感谢您的帮助


Tags: pipinfordefrandomcardtotalplayer
1条回答
网友
1楼 · 发布于 2024-04-19 16:47:06

在调用deal_card()时,看起来deck是空的(即仍然等于[])。如果您在调用create_deck()之前调用deal_card(),应该可以。你知道吗

相关问题 更多 >