https://github.com/JoseMachete/learning_python
Next step would be to compare the dealings to get a winner. The idea's roadmap for that is easy but to write the code for it...
#learningpython #python
@josemachete79 3_card_combo.py looks good. You're going to want to return something from your dealing() function, maybe:
return player_card, computer_card
Then when you call it later, you can do:
player_card, computer_card = dealing()
At this point, maybe I'd point out that I would have called them player_hand and computer_hand, but Python doesn't care.
What each hand will look like is:
[('Five of Spades', 5), ('Ten of Spades', 10), ('Four of Clubs', 4)]
So to compare them, you can sort by value, and then compare with boolean operators. The sorted() function is good here, but by default, will sort by the key, not the value, which won't compare things like you want:
sorted([('Five of Spades', 5), ('Ten of Spades', 10), ('Four of Clubs', 4)]]
will give you:
[('Five of Spades', 5), ('Four of Clubs', 4), ('Ten of Spades', 10)]
So you need to add the key= parameter, which allows you to provide a function to do return a different thing for comparison. Here I use a lambda function, to return just the second item of the tuple - the value part.
sorted([('Five of Spades', 5), ('Ten of Spades', 10), ('Four of Clubs', 4)],key=lambda x: x[1])
This outputs the following, which is sorted on the second item of each tuple:
[('Four of Clubs', 4), ('Five of Spades', 5), ('Ten of Spades', 10)]
Once you have done that, you can just compare them:
# At the end of dealing:
return sorted(player_hand,key=lambda x: x[1]), sorted(computer_hand,lambda x: x[1])
player_card, computer_card = dealing()
if player_hand < computer_hand:
...
elif player_hand == computer_hand:
...
else:
...
@cybervegan ohh that explanation is golden. I like where this exercise is going. Thank you kindly! Good night!
Ps:tried to get the .pop function working so any dealt card is removed from the dict, but I got caught in a mess.
@josemachete79 Ah right, good call - you don't want players getting the same cards! I don't think pop is necessarily the way to do it, because it will be more convoluted than drawing a card, than removing it from the deck.
It's definitely a good exercise - you're learning lots about how data handling works.
Aren't poker hands traditionally dealt alternately, like you, me, you, me, you, me? Sorry I'm not a card player!
If so, I would do something like this:
player_hand = []
computer_hand = []
for deal in range(3):
for hand in (player_hand,computer_hand):
card = random.randint(0,len(full_poker_deck)
card = random.choice(full_poker_deck.items())
full_poker_deck.pop(card[0])
hand.append(card)
But note that this will erode your full_poker_deck dict in the process, so you will need to work on a copy of it if you want to play again without restarting the program. This would do it:
working_deck = dict(full_poker_deck.items())