2017-04-06 10 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() 

のこの種では平凡 だ。また、私はあれば私はかどうかわからないんだけど「ここでは正しく

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

エースをカウントM Iは

を取得するすべてのエラーは、私が繰り返しこれらの電子を得続けるされています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()に電話すると、問題はないはずです。

関連する問題