Python Forum

Full Version: Using two functions to shuffle a deck of cards
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
First, I would like to say that I'm happy to be apart of the community Big Grin . I'm University student, that have recently been having trouble with my program for my assignment. I have spent hours on this without much success. The program is suppose to include two functions, one the defines a function that creates a deck, and another that defines a function that shuffles the deck of cards. I am not allowed to use the built in python function to shuffle the deck of cards. My code is below:
import random
import math

def createDeck():
    spades = ["2s", "3s", "4s", "5s", "6s", "7s", "8s", "9s", "Ts", "Js", "Qs", "Ks", "As"]
    clubs = ["2c", "3c", "4c", "5c", "6c", "7c", "8c", "9c", "Tc", "Jc", "Qc", "Kc", "Ac"]
    diamonds = ["2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d", "Td", "Jd", "Qd", "Kd", "Ad"]
    hearts = ["2h", "3h", "4h", "5h", "6h", "7h", "8h", "9h", "Th", "Jh", "Qh", "Kh", "Ah"]
    deck = spades + clubs + diamonds + hearts
    print(deck)
    return deck

def shuffleDeck(deck):
    amnt_to_shuffle = len(deck) 
    while amnt_to_shuffle > 1: 
             i = int(math.floor(random() * amnt_to_shuffle)) 
             amnt_to_shuffle -= 1 
             deck[i], deck[amnt_to_shuffle] = deck[amnt_to_shuffle], deck[i]

def main():
    shuffleDeck(createDeck())
    return print(shuffleDeck(createDeck()))

My createDeck function works find, but can't seem to pass the deck through my shuffleDeck() to get it to shuffle.
The error in your program is from writing random instead of random.random, however, I think random.randrange(52) is more intuitive regardless.
import random
import itertools


VALUES = "23456789TJQKA"
SUITS = "scdh"


def createDeck():
    return ["".join(card) for card in itertools.product(VALUES, SUITS)]
    
 
def shuffleDeck(deck):
    for i, card in enumerate(deck):
        insert_at = random.randrange(52)
        deck[i], deck[insert_at] = deck[insert_at], deck[i]


def main():
    deck = createDeck()
    print("Before:\n{}".format(deck))
    shuffleDeck(deck)
    print("\nAfter:\n{}".format(deck))


if __name__ == "__main__":
    main()
Thank you for taking your time to help! Big Grin
There is method in random module that is almost named for a deck of card shuffle.
>>> from random import shuffle

>>> deck = [rank + suit for rank in "23456789TJQKA" for suit in "scdh"]
>>> shuffle(deck)
>>> # Deal 5 card
>>> deck[:5]
['4s', '7h', 'Qc', 'Qs', '5c']
(Feb-14-2017, 08:33 AM)snippsat Wrote: [ -> ]There is method in random module that is almost named for a deck of card shuffle.

Yes, he was specifically told he couldn't use it.

Quote: I am not allowed to use the built in python function to shuffle the deck of cards. 
There is another random method which can do it - random.sample(sequence, number_of_samples). If you're allowed to use it. Are you not to for python.shuffle only?
(Feb-14-2017, 10:29 AM)wavic Wrote: [ -> ]There is another random method which can do it - random.sample(sequence, number_of_samples). If you're allowed to use it. Are you not to for python.shuffle only?

Well I'd say it still obviously goes against the goal of the assignment.  But who knows; the teacher might find it clever enough to allow it.
It's not clear whether restriction is for random.shuffle() only or not