Python Forum
Using two functions to shuffle a deck of cards
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using two functions to shuffle a deck of cards
#1
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.
Reply
#2
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()
Reply
#3
Thank you for taking your time to help! Big Grin
Reply
#4
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']
Reply
#5
(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. 
Reply
#6
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?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(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.
Reply
#8
It's not clear whether restriction is for random.shuffle() only or not
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to compare two cards games? python_beginner99 7 3,691 Nov-30-2021, 06:53 PM
Last Post: deanhystad
  sorting a deck of cards (objects in a list) itmustbebunnies 1 7,184 Dec-05-2018, 02:44 AM
Last Post: ichabod801
  Random Shuffle List simon 10 10,075 Oct-24-2016, 04:02 PM
Last Post: simon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020