2

I'm doing a shuffle cards program.At the end i'm using for loop to print out 10 random cards, but i don't know what wrong with it.

On the end deal_card(card), why i put card because my h/w say so, but if you had other answer i'll be good to listen what you say.

This is my program:

import random

def define_cards():
    rank_string = ("ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king")
    suit_string = ("clubs","diamonds","hearts","spades")
    cards = []
    for suit in range(4):
        for rank in range(13):
            card_string = rank_string[rank] + " of " + suit_string[suit]
            cards.append(card_string)
        return cards

def create_deck(deck):
    for i in range(52):
        deck.append(i)
        return

def shuffle_deck(deck):
    random.shuffle(deck)
    return

def deal_card(deck):
    return deck.pop(0)

deck=[]

create_deck(deck)
shuffle_deck(deck)
print "The first 10 cards are:"
for i in range(10):             # I don't know why won't work
    deal_card(card)
    print define_cards()

Print out chould look like:

The first 10 cards are:
queen of hearts
ten of diamonds
...
phhnk
  • 121
  • 2
  • 4
  • 10
  • You can use python's random.shuffle to shuffle your deck. [Python - Shuffling List of Objects](https://stackoverflow.com/questions/976882/shuffling-a-list-of-objects) – jellyjj Mar 11 '18 at 15:42

3 Answers3

6

Since your define_cards already produces a list of card names, you should use that to generate the deck instead of create_deck. Then in the for loop just deal a card and print it.

deck = define_cards()
shuffle_deck(deck)
print "The first 10 cards are:"
for i in range(10):
    card = deal_card(deck)
    print card

Just doing this makes the program print ten cards from the top of the deck. However, define_cards still has a little bug. Can you spot it? Hint: positioning of return.

  • Isn't should be only 4 space, same level as `for suit in range(4)` and under `cards.append(card_string)` or below `cards.append(card_string)` whit 12 space??I try is work the both way, what is the different?? – phhnk May 07 '11 at 07:31
  • @phhnk: Depending on the indentation, the `return` is either inside one of the `for` loops or after both of them. You'll want it after both `for` loops have completed, so 4 spaces. With the version in your question (8 spaces) you only get the *clubs* cards. – StackExchange saddens dancek May 07 '11 at 07:43
3

Half of the code is superfluous:

>>> import random
>>> deck = range(1,52)
>>> random.shuffle(deck)
>>> deck
[4, 38, 40, 18, 35, 44, 50, 22, 49, 26, 8, 45, 14, 20, 25, 34, 37, 51, 42, 29, 24, 28, 27, 30, 7, 47, 23, 3, 10, 2, 9, 39, 6, 16, 12, 17, 11, 41, 33, 48, 5, 1, 36, 21, 13, 32, 43, 19, 15, 31, 46]
  • I know i didn't make so cleanly the question, but i still appreciate your help, tho. THANK – phhnk May 07 '11 at 07:07
1

You might want to think about creating a class Card and an class Deck to maintain a deck of cards. This will give you clearer code.

ncmathsadist
  • 4,684
  • 3
  • 29
  • 45