#๐Ÿ”’ Issue on how to get count property to print out amount in list correctly and create list.

77 messages ยท Page 1 of 1 (latest)

fickle sierra
#

Hello again, I am working on a Deck class for a card game and I ran into a brick wall. I have no idea how to get the count function to correctly count what's in the list (which is currently empty). I am also having an issue understanding how to create said list to be filled with items. The hint I was given was to use a loop and another list, but I do not understand it.

My current codes:
Deck class:

class Deck:
    def __init__(self):
        self.__deck = []

    def add52(self, new_deck):
        self.__deck.append(new_deck)

    @property
    def count(self):
        return self.__deck
    
    def shuffle(self):
        random.shuffle(self.__deck)
        return self.__deck

    def dealCard(self):
        return self.__deck.pop()```
NOTE: There are other classes, but I have purposely left them out as they are irrelevant

Main() function (in case you need it. This is used to test the classes and I need it to be able to run through this):
```py
def main():
    print("Cards - Tester")
    print()

    #test deck
    print("DECK")
    deck = Deck()
    print("Deck created.")
    deck.shuffle()    
    print("Deck shuffled.")
    print("Deck count:", deck.count)
    print()

if __name__ == "__main__":
    main()```
NOTE: There was another block here for the Hand class, but I have left that out for the same reason as before.
solemn treeBOT
#

@fickle sierra

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

stone acorn
#

don't you want deck.count to return the length of the deck instead of the deck

fickle sierra
#

I thought I mentioned that...

#

but yes, I do want the deck.count to return the length of the deck instead of the deck itself

#

I should mention, I cannot alter the main function if that's what you're thinking

stone acorn
#

!e the len function returns the length of a list

deck = [1, 2, 3]
print(len(deck))
solemn treeBOT
#

@stone acorn :white_check_mark: Your 3.12 eval job has completed with return code 0.

3
fickle sierra
#

oh

#

I was thinking about the len() function, but I had no idea how to implement that using the return call

stone acorn
#

just return len(self.__deck)

fickle sierra
#

huh...

#

why didn't it work when I tried that...?

stone acorn
#

no idea

fickle sierra
#

either I made a typo or put it in the wrong spot

stone acorn
#

i guess the main issue now is that your deck is empty when you create it

fickle sierra
#

yeah

stone acorn
#

do you know what items will go in it?

fickle sierra
#

the idea I have though, is creating two other lists

#

one has 4 values and the other has 13

#

I basically loop that until all possible variations have been created

stone acorn
#

like, will deck look like:

["AH", "AC", "1D", ...]
#

or tuples:

[("Ace", "Hearts"), ("Ace", "Clubs"), ...]
fickle sierra
#

The deck should look like...

["King of Spades", "Aces of Spades", "Queen of Diamonds", ...]```
#

though tuples also works

stone acorn
#

your idea will work, a list of values and a list of suits

fickle sierra
#

I know it will because it was left there as a hint

stone acorn
#

will require a double loop to create all the cards

fickle sierra
#

I see...

#

hmm...

stone acorn
#

!e

suits = ["Leaves", "Rocks"]
values = ["1", "2"]
for suit in suits:
    for value in values:
        print(suit, value)
solemn treeBOT
#

@stone acorn :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Leaves 1
002 | Leaves 2
003 | Rocks 1
004 | Rocks 2
fickle sierra
#

would it use...

for i in range(number here):
    append here```?
stone acorn
#

you can iterate directly over the list like above

fickle sierra
#

ah

#

I see

stone acorn
#

instead of printing the items, you can concatenate them because they're strings

#

!e

suits = ["Leaves", "Rocks"]
values = ["1", "2"]
for suit in suits:
    for value in values:
        card = suit + value
        print(card)
solemn treeBOT
#

@stone acorn :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | Leaves1
002 | Leaves2
003 | Rocks1
004 | Rocks2
stone acorn
#

you might want a space in there, or maybe " of "

#

could try

card = value + " of " + suit

instead

fickle sierra
#

oh, I took another look and I wouldn't look at the stuff I want it to look like

#

it should look more like the tuple than the latter

stone acorn
#

!e ok, then you can still join the items together, just like this instead:

suits = ["Leaves", "Rocks"]
values = ["1", "2"]
for suit in suits:
    for value in values:     
        card = (value, suit)
        print(card)
solemn treeBOT
#

@stone acorn :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | ('1', 'Leaves')
002 | ('2', 'Leaves')
003 | ('1', 'Rocks')
004 | ('2', 'Rocks')
stone acorn
#

the (value, suit) creates a tuple

fickle sierra
#

I had something similar

#

from what you have told me

#

thank you though

stone acorn
#

np

fickle sierra
#

that's... odd...

#

it's only creating 19

#

it should create all 52

stone acorn
#

can you paste your code?

fickle sierra
#

My bad

def __init__(self):
        self.__deck = []
        suit = ["Spades", "Hearts", "Diamonds", "Clubs"]
        rank = [1, 2, 3, 4, 5, 6, 7, 8, 9, "Jack", "Queen", "King", "Aces"]
        for suit in suit:
            for rank in rank:
                card = (suit, rank)
                self.__deck.append(card)```
#

hold on...

stone acorn
#

ahh, rename your first rank to ranks

#

and then for rank in ranks

fickle sierra
#

oh

stone acorn
#

same with suits actually

fickle sierra
#

I had that earlier, but VSC flagged it as a problem (not actually flagging it, but dimming it if you get what I mean)

stone acorn
#

whats happening is suit is getting redefined

#

and rank is getting redefined

#

vsc is probably highlighting it because its shadowed

#
def __init__(self):
        self.__deck = []
        suits = ["Spades", "Hearts", "Diamonds", "Clubs"]
        ranks = [1, 2, 3, 4, 5, 6, 7, 8, 9, "Jack", "Queen", "King", "Aces"]
        for suit in suits:
            for rank in ranks:
                card = (suit, rank)
                self.__deck.append(card)
#

notice the plurals

fickle sierra
#

I see

#

ok, I think I got what I need

#

thanks for your help

stone acorn
#

you're welcome!

#

good luck!

fickle sierra
#

thank you

#

have a good day

#

or evening

#

!close

solemn treeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.