0

I want to return an array composed of string type characters from my list and currently my program is just printing out each iteration straight to the shell this is my current output:

deck = Deck()
print(deck)
2 S, 
3 S, 
4 S, 
5 S, 
6 S, 
7 S, 
8 S, 
9 S, 
T S, 
J S, 
Q S, 
K S, 
A S, 
2 C, 
3 C, 
4 C, 
5 C, 
6 C, 
7 C, 
8 C, 
9 C, 
T C, 
J C, 
Q C, 
K C, 
A C, 
2 D, 
3 D, 
4 D, 
5 D, 
6 D, 
7 D, 
8 D, 
9 D, 
T D, 
J D, 
Q D, 
K D, 
A D, 
2 H, 
3 H, 
4 H, 
5 H, 
6 H, 
7 H, 
8 H, 
9 H, 
T H, 
J H, 
Q H, 
K H, 
A H

**the output I need is: **

2 C, 3 C, 4 C, 5 C, 6 C,
7 C, 8 C, 9 C, T C, J C,
Q C, K C, A C, 2 D, 3 D,
4 D, 5 D, 6 D, 7 D, 8 D,
9 D, T D, J D, Q D, K D,
A D, 2 H, 3 H, 4 H, 5 H,
6 H, 7 H, 8 H, 9 H, T H,
J H, Q H, K H, A H, 2 S,
3 S, 4 S, 5 S, 6 S, 7 S,
8 S, 9 S, T S, J S, Q S,
K S, A S

I have tried implementing for loops and counters to no success... here is my code:

import random
class Card:
    def __init__(self, rank, suit): # initialize number variables here
        self._rank = rank
        self._suit = suit
        
    
    def __str__(self): #overload this to get a readable string representation of our card object
        return str(self._rank) + ' ' + str(self._suit)
    
    def __eq__(self, other):
        if self._rank == other._rank:
            return True
    
    def __ne__(self, other):
        if self._rank != other._rank:
            return True

class Deck:
    def __init__(self): 
        # create a list of card objects only need to pass in self
        #intialize a string or list of suits
        #intialize a string or list of ranks
        # building the list of cards
        
        self._deck = []
        self._dealt = []
        suits = ['S', 'C', 'D', 'H']
        ranks = ['2', '3' , '4', '5', '6', '7' , '8', '9','T', 'J' , 'Q', 'K', 'A']
        for suit in suits:
            for rank in ranks:
                self._deck.append(Card(rank,suit))
        
        
    def __str__(self):
        return ', \n'.join(str(i) for i in self._deck)

Thankyou any help will certainly be appreciated.

That Guy
  • 17
  • 5

3 Answers3

1

The brute force way is as a loop:

    def __str__(self):
        s = []
        for n in range(0,len(self._deck),5):
            s.append( ' '.join(str(i) for i in self._deck[n:n+5] )
        return '\n'.join(s)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

See How do you split a list into evenly sized chunks? for grouping the deck into chunks of 5 cards. Than you can join the cards in a chunk with , , and join the chunks with ,\n.

def __str__(self):
    n = 5
    return ',\n'.join(', '.join(self._deck[i:i + n]) for i in range(0, len(self._deck), n))
Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

One way is to use a logical sequencing operation in for-loop that keeps count of no. of output per line:

ctr=0
for d in deck:
   ctr+=1
   print(d,end=', ')
   #check if max 5 characters are printed in each line
   if ctr==5:
      #print new line
      print()
      #init ctr 
      ctr=ctr%5
      

This approach can be used during printing characters to stdout.

projjal
  • 264
  • 3
  • 10