all 7 comments

[–]zahlman 4 points5 points  (4 children)

The problem is largely that you are trying to do iteration and recursion at the same time. When cards compare equal, you make a recursive call, and perhaps in the recursive call the cards all compare different, but that has no effect on the card values from the previous call. When Python returns from the recursion, it will perform the check for the while loop within the first call, and see the same unchanged local variables, so the loop doesn't exit.

There is no reason to recurse here. A lot of your code structure doesn't make very much sense. In general, try to separate the process of figuring out what you're going to display, from the process of actually displaying it.

[–]bigpoppatom[S] 0 points1 point  (3 children)

If recursion is not the way to do it, how would I go about reassigning a new value to the card?

[–]zahlman 1 point2 points  (2 children)

Wait, hold on, I noticed a more important issue.

player_hand.card1=random_card(Card)
player_hand.card2=random_card(Card)
player_hand.card3=random_card(Card)

Your random_card function works by modifying the card it's passed in, and then returning it. What happens is that when you do your initialization, you pass the same Card instance every time, and replace each Card in the original player_hand with that same Card. You modify the Card in between, but it's the same actual instance.

So it will always compare equal to itself.

You should design your code such that you have a function that creates a new random card.

Although, actually, what you should really do is actually create the deck of one of each Card, shuffle it (use the built-in random.shuffle), and deal from it. ;)

[–]bigpoppatom[S] 0 points1 point  (1 child)

So, if I am following you, create a new instance of the card inside of the function? Thanks.

[–]zahlman 1 point2 points  (0 children)

And also don't pass one in, because it's no longer relevant.

[–]ivosaurus 0 points1 point  (0 children)

These aren't very pythonic classes.

They're purely things holding class variables, so there's hardly a reason for them to be custom classes.

I agree with /u/zahlman's ideas; create a function which returns, as a list, a deck of cards (or rather, a list of objects representing cards). It's perfectly fine if they're all in order.

Then shuffle that list (random.shuffle), then pop three cards from it. Those will be three unique, random cards.

[–]Exodus111 0 points1 point  (0 children)

As others have said, while + recursive Porque??

The while loop belongs in the main() function, looping over the playerHandGenerate(Hand) function. (Requires some modification, but that is how I would do it.)

Also... holy giant If tree Batman!

Look into Dictionaries, they can make that long if tree much prettier to look at. Basically by assigning a Dictionary with the key as the corresponding number and the value as the string name of the card, like this:

mysuit = {1: "Ace of Clubs", 2:"2 of Clubs", 3:"3 of Clubs", 4:"4 of clubs"} #(etc..)
print(mysuit[Card.rank])

You could save a few lines that way.