2017-01-02 42 views
-2

これは私のブラックジャックのゲームであり、私はそれを実行するたびに、私はこのエラーを取得:ブラックジャックの試合にはエラーがありますか?

Traceback (most recent call last): 
    File "...", line 42, in <module> 
    mydeck = deck() 
    File "...", line 9, in deck 
    deck.append(suit+rank) 
TypeError: Can't convert 'int' object to str implicitly 

(私は、ファイルの場所と名前を取った)

なぜこの私はわからないんだけど起こっている。誰かが助けてくれますか?ありがとう!

# Blackjack Game 

import random 

def deck(): 
    deck = [] 
    for suit in ['H','S','D','C']: 
     for rank in ['A',2,3,4,5,6,7,8,9,10,'J','Q','K']: 
      deck.append(suit+rank) 
    random.shuffle(deck) 
    return deck 

def pCount(cards): 
    count = 0 
    aceCount = 0 
    for i in cards: 
     if(i[1] == 'J' or i[1] == 'Q' or i[1] == 'K' or i[1] == 10): 
      count += 10 
     elif (i[1] != 'A'): 
      count += int(i[1]) 
     else: 
      aceCount += 1 
    if aceCount == 1 and count >= 10: 
     count += 11 
    elif aceCount != 0: 
     count += 1 
    return count 

def playingHands(deck): 
     dealerhand = [] 
     playerhand = [] 
     dealerhand.append(deck.pop()) 
     dealerhand.append(deck.pop()) 
     playerhand.append(deck.pop()) 
     playerhand.append(deck.pop()) 

     while pCount(dealerhand) <= 16: 
      dealerhand.append(deck.pop()) 
     return [dealerhand, playerhand] 

game = "" 
mydeck = deck() 
hands = playingHands(dck) 
dealer = hands[0] 
player = hands[1] 

while game != 'exit': 
    dealerCount = pCount(dealer) 
    playerCount = pCount(player) 
    print ('Dealer has: ') 
    print (dealer) 
    print ('Player, you have: ') 
    print (player) 
    if playerCount == 21: 
     print ('Blackjack! Player wins!') 
     break 
    elif playerCount > 21: 
     print ('Player busts! With '+playerCount+' points. Dealer wins!') 
     break 
    elif dealerCount > 21: 
     print ('Dealer busts! With '+dealerCount+' points. Player wins!') 
     break 
    game = input('What would you like to do? H: hit, S: stand? ') 
    if game == 'H': 
     player.append(deck.pop()) 
    elif playerCount > dealerCount: 
     print ('Player wins with ' + playerCount + ' points') 
     print ('Dealer has ' + dealer + ' or ' +dealerCount + ' points') 
     break 
    else: 
     print ('Dealer wins!') 
     print ('Dealer has ' + dealer + ' or ' +dealerCount + ' points')   

。 。 。 。 。 。 。 。 。 .. 。 。 。 。 。 。 。

.. 。 。 。 。 。 。 。

+0

なぜ最後に変なドットのthats?また、printと関数の引数の間に空白を入れないでください。 – Lucas

答えて

1

私はできる限り修正して試してみました。今私はそれを再生することができます。

import random 

def deck(): 
    deck = [] 
    for suit in ['H','S','D','C']: 
     for rank in ['A',2,3,4,5,6,7,8,9,10,'J','Q','K']: 
      deck.append(suit+str(rank)) 
    random.shuffle(deck) 
    return deck 

def pCount(cards): 
    count = 0 
    aceCount = 0 
    for i in cards: 
     if(i[1] == 'J' or i[1] == 'Q' or i[1] == 'K' or i[1] == 10): 
      count += 10 
     elif (i[1] != 'A'): 
      count += int(i[1]) 
     else: 
      aceCount += 1 
    if aceCount == 1 and count >= 10: 
     count += 11 
    elif aceCount != 0: 
     count += 1 
    return count 

def playingHands(deck): 
     dealerhand = [] 
     playerhand = [] 
     dealerhand.append(deck.pop()) 
     dealerhand.append(deck.pop()) 
     playerhand.append(deck.pop()) 
     playerhand.append(deck.pop()) 

     while pCount(dealerhand) <= 16: 
      dealerhand.append(deck.pop()) 
     return [dealerhand, playerhand] 

game = "" 
mydeck = deck() 
hands = playingHands(mydeck) 
dealer = hands[0] 
player = hands[1] 

while game != 'exit': 
    dealerCount = pCount(dealer) 
    playerCount = pCount(player) 
    print ('Dealer has: ') 
    print (dealer) 
    print ('Player, you have: ') 
    print (player) 
    if playerCount == 21: 
     print ('Blackjack! Player wins!') 
     break 
    elif playerCount > 21: 
     print ('Player busts! With '+str(playerCount)+' points. Dealer wins!') 
     break 
    elif dealerCount > 21: 
     print ('Dealer busts! With '+str(dealerCount)+' points. Player wins!') 
     break 
    game = raw_input('What would you like to do? H: hit, S: stand? ') 
    if game == 'H': 
     player.append(deck().pop()) 
    elif playerCount > dealerCount: 
     print ('Player wins with ' + playerCount + ' points') 
     print ('Dealer has ' + str(dealer) + ' or ' +str(dealerCount) + ' points') 
     break 

    else: 
     print ('Dealer wins!') 
     print ('Dealer has ' + str(dealer) + ' or ' +str(dealerCount) + ' points') 

テスト

$ python pyprog.py 
Dealer has: 
['H2', 'S8', 'HJ'] 
Player, you have: 
['HK', 'C3'] 
What would you like to do? H: hit, S: stand? S 
Dealer wins! 
Dealer has ['H2', 'S8', 'HJ'] or 20 points 
Dealer has: 
['H2', 'S8', 'HJ'] 
Player, you have: 
['HK', 'C3'] 
What would you like to do? H: hit, S: stand? S 
Dealer wins! 
Dealer has ['H2', 'S8', 'HJ'] or 20 points 
Dealer has: 
['H2', 'S8', 'HJ'] 
Player, you have: 
['HK', 'C3'] 
What would you like to do? H: hit, S: stand? H 
Dealer has: 
['H2', 'S8', 'HJ'] 
Player, you have: 
['HK', 'C3', 'H6'] 
What would you like to do? H: hit, S: stand? S 
Dealer wins! 
Dealer has ['H2', 'S8', 'HJ'] or 20 points 
Dealer has: 
['H2', 'S8', 'HJ'] 
Player, you have: 
['HK', 'C3', 'H6'] 
What would you like to do? H: hit, S: stand? H 
Dealer has: 
['H2', 'S8', 'HJ'] 
Player, you have: 
['HK', 'C3', 'H6', 'H6'] 
Player busts! With 25 points. Dealer wins! 
0

問題は、intにstrを追加することです(エラーが示すように)。 strに変換してください。

my_list = ['A',2,3,4,5,6,7,8,9,10,'J','Q','K'] 

for i in my_list: 
    print(i) 
# Return the error 

for i in my_list: 
    print(str(i)) 

# print the list 
関連する問題