2017-04-06 123 views
-1
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() 

我认为它要么这些功能,但我是一个平庸的 在这种东西python3黑杰克指数误差

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

而且,我不知道如果我,如果我“M计数的王牌正确

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

这里是我得到的

的错误,我反复不断收到这些电子邮件rrors

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 

任何帮助感激

+0

错误(回溯)不言自明:“IndexError:无法从空序列中选择”。从你的代码(至少是你共享的):'card = random.choice(deck)','deck = []'。可能你应该在初始化'deck'为空列表后调用'create_deck()'。 – CristiFati

回答

-1

貌似甲板是空的(即仍然等于[]),当您拨打deal_card()时。如果您在致电deal_card()之前致电create_deck(),您应该没问题。