Skip to main content
Question Protected by Mast
Added edit section at end of question
Source Link
AJ123
  • 143
  • 1
  • 1
  • 8

EDIT - Thanks to you all. If I could accept all of the answers, I would. One small thing - I noticed that, when I made a separate python file with all improvements suggested, the file size went down from 4 KB to 3 KB! With this check mark, assume it is all of your answers I am accepting :P
In the following code, I used something from all of your answers as a small thank you. Please feel free to try the code yourself!
New Code:

import random

def get_deck():
    '''
    Funtion which randomly selects the cards from a full
    deck of 52 cards previously generated.
    Parameters: None
    Returns: a list of 21 cards
    '''
    ranks = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
    suits = [u'\u2665',u'\u2663',u'\u2666',u'\u2660']
    full_deck = [r+s for s in suits for r in ranks] # Creates a full deck of 52 cards
    return random.sample(full_deck, 21) # returns a random sample of 21 cards from the deck


def get_piles(deck):
    '''
    Funtion which deals the cards in the way that a magician would.
    It then adds the cards to three lists called pile_1, pile_2 and pile_3, depending
    on their position in the deck.
    Parameters: deck
    Returns: pile_1, pile_2, pile_3
    ''' 
    pile_1 = deck[0::3] # Used python's list slicing to split into piles
    pile_2 = deck[1::3] # rather than the for loop used before
    pile_3 = deck[2::3]
    return pile_1, pile_2, pile_3


def get_newdeck(choice, pile_1, pile_2, pile_3):
    '''
    Function which reorders the deck in a way that the chosen list is
    in the middle of the two other lists.
    Parameters: choice, pile_1, pile_2, pile_3
    Returns: the new, reordered deck
    '''
    deck = pile_1+pile_3+pile_2
    if choice == 1:
        deck = pile_2+pile_1+pile_3
    elif choice == 2:
        deck = pile_1+pile_2+pile_3
    return deck


def print_piles(pile_1, pile_2, pile_3):
    '''
    Procedure which prints the lists(pile_1, pile_2, and pile_3) vertically
    with headers to tell the user which piles they are.
    Parameters: pile_1, pile_2, pile_3
    Returns: None
    '''
    print("Pile 1\tPile 2\tPile 3")        
    for i in range(7):                
        print(pile_1[i]+'\t'+pile_2[i]+'\t'+pile_3[i])


def get_choice():
    '''
    Funtion which gets the user's input of which pile their chosen
    cards is inside. It also tells the user if they entered an invalid
    input (not 3, 2, or 1). It also allows the user to quit the program.
    Parameters: None
    Returns: choice
    '''
    while True:
         try: 
             choice = int(input("Which pile is your card in? (1-3), press CTRL + C to quit? > "))            
         except ValueError:
             print("Must be an integer")
         except KeyboardInterrupt: # When user presses Ctrl + C
            print("\nThanks... good bye!")
            quit(0) # Terminates the program
         else:
            if 1 <= choice <= 3:
                break
            else:
                print("Must be between 1 and 3")               
    return choice


def main():
    '''
    The main body of the code, using previous procedures and functions
    to make the code work as intended.
    Parameters: None
    Returns: None
    '''
    deck = get_deck() 
    print("Choose a card and remember it")
    for x in range(3):
        pile_1, pile_2, pile_3 = get_piles(deck) # Changed function and variable names
        print_piles(pile_1, pile_2, pile_3)
        choice = get_choice()
        print()
        deck = get_newdeck(choice, pile_1, pile_2, pile_3)
    print()
    print('>>> Your card is {} <<<'.format(deck[10]))


if __name__ == '__main__': 
    main()

EDIT - Thanks to you all. If I could accept all of the answers, I would. One small thing - I noticed that, when I made a separate python file with all improvements suggested, the file size went down from 4 KB to 3 KB! With this check mark, assume it is all of your answers I am accepting :P

EDIT - Thanks to you all. If I could accept all of the answers, I would. One small thing - I noticed that, when I made a separate python file with all improvements suggested, the file size went down from 4 KB to 3 KB! With this check mark, assume it is all of your answers I am accepting :P
In the following code, I used something from all of your answers as a small thank you. Please feel free to try the code yourself!
New Code:

import random

def get_deck():
    '''
    Funtion which randomly selects the cards from a full
    deck of 52 cards previously generated.
    Parameters: None
    Returns: a list of 21 cards
    '''
    ranks = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
    suits = [u'\u2665',u'\u2663',u'\u2666',u'\u2660']
    full_deck = [r+s for s in suits for r in ranks] # Creates a full deck of 52 cards
    return random.sample(full_deck, 21) # returns a random sample of 21 cards from the deck


def get_piles(deck):
    '''
    Funtion which deals the cards in the way that a magician would.
    It then adds the cards to three lists called pile_1, pile_2 and pile_3, depending
    on their position in the deck.
    Parameters: deck
    Returns: pile_1, pile_2, pile_3
    ''' 
    pile_1 = deck[0::3] # Used python's list slicing to split into piles
    pile_2 = deck[1::3] # rather than the for loop used before
    pile_3 = deck[2::3]
    return pile_1, pile_2, pile_3


def get_newdeck(choice, pile_1, pile_2, pile_3):
    '''
    Function which reorders the deck in a way that the chosen list is
    in the middle of the two other lists.
    Parameters: choice, pile_1, pile_2, pile_3
    Returns: the new, reordered deck
    '''
    deck = pile_1+pile_3+pile_2
    if choice == 1:
        deck = pile_2+pile_1+pile_3
    elif choice == 2:
        deck = pile_1+pile_2+pile_3
    return deck


def print_piles(pile_1, pile_2, pile_3):
    '''
    Procedure which prints the lists(pile_1, pile_2, and pile_3) vertically
    with headers to tell the user which piles they are.
    Parameters: pile_1, pile_2, pile_3
    Returns: None
    '''
    print("Pile 1\tPile 2\tPile 3")        
    for i in range(7):                
        print(pile_1[i]+'\t'+pile_2[i]+'\t'+pile_3[i])


def get_choice():
    '''
    Funtion which gets the user's input of which pile their chosen
    cards is inside. It also tells the user if they entered an invalid
    input (not 3, 2, or 1). It also allows the user to quit the program.
    Parameters: None
    Returns: choice
    '''
    while True:
         try: 
             choice = int(input("Which pile is your card in? (1-3), press CTRL + C to quit? > "))            
         except ValueError:
             print("Must be an integer")
         except KeyboardInterrupt: # When user presses Ctrl + C
            print("\nThanks... good bye!")
            quit(0) # Terminates the program
         else:
            if 1 <= choice <= 3:
                break
            else:
                print("Must be between 1 and 3")               
    return choice


def main():
    '''
    The main body of the code, using previous procedures and functions
    to make the code work as intended.
    Parameters: None
    Returns: None
    '''
    deck = get_deck() 
    print("Choose a card and remember it")
    for x in range(3):
        pile_1, pile_2, pile_3 = get_piles(deck) # Changed function and variable names
        print_piles(pile_1, pile_2, pile_3)
        choice = get_choice()
        print()
        deck = get_newdeck(choice, pile_1, pile_2, pile_3)
    print()
    print('>>> Your card is {} <<<'.format(deck[10]))


if __name__ == '__main__': 
    main()
Added edit section at end of question
Source Link
AJ123
  • 143
  • 1
  • 1
  • 8

EDIT - Thanks to you all. If I could accept all of the answers, I would. One small thing - I noticed that, when I made a separate python file with all improvements suggested, the file size went down from 4 KB to 3 KB! With this check mark, assume it is all of your answers I am accepting :P

EDIT - Thanks to you all. If I could accept all of the answers, I would. One small thing - I noticed that, when I made a separate python file with all improvements suggested, the file size went down from 4 KB to 3 KB! With this check mark, assume it is all of your answers I am accepting :P

deleted 12 characters in body; edited tags; edited title
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Suggest potential improvements for my code Simulated card trick with splitting and reordering

Any ideas? FYI no improvements regarding commenting please! Believe me - I tried incredibly hard to find anything, my best guess was to try to improve the speed, but it already runs pretty well. I suppose I could use some different data structures to make my code more concise. Good luck!

Suggest potential improvements for my code

Any ideas? FYI no improvements regarding commenting please! Believe me - I tried incredibly hard to find anything, my best guess was to try to improve the speed, but it already runs pretty well. I suppose I could use some different data structures to make my code more concise. Good luck!

Simulated card trick with splitting and reordering

Any ideas? FYI no improvements regarding commenting please! Believe me - I tried incredibly hard to find anything, my best guess was to try to improve the speed, but it already runs pretty well. I suppose I could use some different data structures to make my code more concise.

Source Link
AJ123
  • 143
  • 1
  • 1
  • 8
Loading