Poker hand string display
I'm brand new to programming and Python and I'm trying my hardest to understand and learn it. I'm not asking for answers but explanations in plain non-computer terms so that i can try to work out the solution myself.
Here is one more problem I'm having. I have 4 lists below:
short_card = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
long_card = ['ace', 'king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven', 'six', 开发者_开发知识库'five', 'four', 'three', 'deuce']
short_suit = ['c', 'd', 'h', 's']
long_suit = ['clubs', 'diamonds', 'hearts', 'spades']
Now what im supposed to do is write two functions: card_str(c) and hand_str(h).
card_str(c) takes a two character string and searches to find out the corresponding characters to display the card in text. For instance if I put 'kh' the program will output "King of Hearts".
hand_str(h) takes a list of two character strings and displays the appropriate hand in full text. Again for instance if I put (["Kh", "As", "5d", "2c"]), it will output "King of Hearts, Ace of Spades, Five of Diamonds, Deuce of Clubs".
Below is what I have so far:
short_card = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
long_card = ['ace', 'king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven', 'six', 'five', 'four', 'three', 'deuce']
short_suit = ['c', 'd', 'h', 's']
long_suit = ['clubs', 'diamonds', 'hearts', 'spades']
def card_str(c):
def hand_str(h):
#- test harness: do not modify -#
for i in range(13):
print card_str(short_card[i] + short_suit[i%4])
l = []
for i in range(52):
l.append(short_card[i%13] + short_suit[i/13])
print hand_str(l)
You don't have much, but I'll say that your lists are in pairs.
short_card = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
long_card = ['ace', 'king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven', 'six', 'five', 'four', 'three', 'deuce']
and
short_suit = ['c', 'd', 'h', 's']
long_suit = ['clubs', 'diamonds', 'hearts', 'spades']
They're each the same length and in the same order. So the index of 'A' in short_card is the same as the index of 'ace' in long_card. So if you find the index of one, you have the index of the other.
This should point you in the right direction. Come back and edit your post when you have more.
So what you have is two sets of lists, which correlate the input values with the output strings. Note the order of the lists; they're the same. Which should make the index values of the inputs equal to the...
I'd do it slightly differently to the last two posters, and start with the zip
function for joining up matching lists.
>>> def poker_card(c):
... card, suit = c
... short_cards = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
... short_suits = ['c', 'd', 'h', 's']
... long_cards = ['ace', 'king', 'queen', 'jack', 'ten', 'nine', 'eight', 'seven', 'six', 'five', 'four', 'three', 'deuce']
... long_suits = ['clubs', 'diamonds', 'hearts', 'spades']
... return "%s of %s" % (long_cards[short_cards.index(card)], long_suits[short_suits.index(suit)])
...
>>> def poker_hand(hand):
... return [poker_card(c) for c in hand]
...
>>> def main():
... print poker_hand(["Kh", "As", "5d", "2c"])
...
>>> main()
['king of hearts', 'ace of spades', 'five of diamonds', 'deuce of clubs']
精彩评论