Home > Blockchain >  Resolving TypeError: can only concatenate tuple (not "str") to tuple
Resolving TypeError: can only concatenate tuple (not "str") to tuple

Time:12-05

So I am trying to create a blackjack game in python but I get a type error that I don't know how to resolve. I created a class for cards that would make it easy for me to print out the name of the individual cards but I also want to be able to print the names of the cards inside the deck. For this, I thought I would use the string method I used in the Card class.

import random

symbols = ('Spades','Diamonds','Hearts',"Clubs")
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 
            'Nine':9, 'Ten':10, 'Jack':11, 'Queen':12, 'King':13, 'Ace':14}

playing = True

class Card:
    
    def __init__(self,symbol=0,rank=0):
        self.rank = ranks
        self.symbol = symbol
        value = values [rank]
    
    def __str__(self):
        return self.rank   "of"   self.symbol

class Deck:
    
    def __init__(self):
        self.deck = []  # start with an empty list
        for symbol in symbols:
            for rank in ranks:
                self.deck.append(Card(symbol,rank))  # build Card objects,add to list
    
    def __str__(self):
        deck_comp = ''  # start with an empty string
        for Card in self.deck:
            deck_comp  = '\n'  Card.__str__() # add each Card object's print string
        return 'The deck has:'   deck_comp

    def shuffle(self):
        random.shuffle(self.deck)
        
    def deal(self):
        single_card = self.deck.pop()
        return single_card

test_deck = Deck()
print(test_deck)

And here is the error message I get. I understand that Card.str() is probably saved as a tuple but how do I resolve this problem?

Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-1bfc314b8e70> in <module>
      1 test_deck = Deck()
----> 2 print(test_deck)

<ipython-input-40-36ad41485748> in __str__(self)
     10         deck_comp = ''  # start with an empty string
     11         for Card in self.deck:
---> 12             deck_comp  = '\n'   Card.__str__() # add each Card object's print string
     13         return 'The deck has:'   deck_comp
     14 

<ipython-input-31-543b708a921e> in __str__(self)
      7 
      8     def __str__(self):
----> 9         return self.rank   "of"   self.symbol

TypeError: can only concatenate tuple (not "str") to tuple

CodePudding user response:

in your variable self.rank you have tuple and in the variable self.symbol you have string

you need to convert a tuple to a string, jou can use python join method :

return ', '.join(self.rank)   "of"   self.symbol

CodePudding user response:

I think you have typo at Card __init__(). Try self.rank = rank instead self.rank = ranks.

I suppose it is not the only one problem of your code, but it should be workaround for you.

  • Related