0

I'm doing the basic Python card trick and what I've searched for is too complicated, here's my code:

import random
import itertools
deck = ["S A","S 2","S 3","S 4","S 5","S 6","S 7","D A","D 2","D 3","D 4","D 
5","D 6","D 7","C A","C 2","C 3","C 4","C 5","C 6","C 7"]
random.shuffle(deck)
for z in range(3):
    print("Pile ",[z+1])
    for i in range(7):
        print(deck[i])

What I am trying to do is get it so when I get my 3 piles, I don't want any duplicate values but I can't work out how I do it, or how I get them in 3 separate columns?

  • please provide the output you expect from the input you've givon. – Setop Apr 17 '18 at 09:10
  • I should get all the items from the deck in 3 piles, with no duplicates. So Pile 1 should have 7 of the items in deck, Pile 2 would have a different 7 and Pile 3 should have the last 7, but all random so Pile 1 isn't just all the cards from spades. – Randex Grandma Apr 17 '18 at 09:14
  • deck already contains unique values, all you need to do is just split it into 3 piles. Currently you are just shuffling the deck and printing the first 7 cards of the shuffled deck for three times... – Elijan9 Apr 17 '18 at 09:16
  • print(deck[:7]) print(deck[7:14]) print(deck[14:21]) why don't you just slice your already shuffled list? – Mufeed Apr 17 '18 at 09:17
  • Alright, how can I get these into columns to make it neater? – Randex Grandma Apr 17 '18 at 09:25
  • I added a script in answer section to make it the way you want . – Mufeed Apr 17 '18 at 09:27
  • Possible duplicate of [How do you split a list into evenly sized chunks?](https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks) – mkrieger1 Apr 17 '18 at 09:32

4 Answers4

0

use

piles=[deck[0::3],deck[1::3],deck[2::3]]

this build 3 lists starting at position 0, 1 and 2 respectively, jumping from item to item with a length of 3. no need to know the size of the initial list.

  • first picks up items 0, 3, 6, ...
  • second picks up items 1, 4, 7, ...
  • third picks up items 2, 5, 8, ...
Setop
  • 2,262
  • 13
  • 28
  • Nice idea, but how do you specify that each pile will contain 7 cards? – mkrieger1 Apr 17 '18 at 09:31
  • @mkrieger1, you don't have to. The pickup will end with the end of the "deck" list. If deck is only 20 items, third list will be only 6 elements (7+7+6). The same way you distribute cards to players. – Setop Apr 17 '18 at 09:35
0

Slight modification in your code.

n = 0     # starting index of pile 1
for i in range(3):
    print("Pile ",[i+1])
    for j in range(n,n+7):
        print(deck[j])
    n = n+7     # update the index to next pile
Mufeed
  • 3,018
  • 4
  • 20
  • 29
0

Try this:

import random

deck = ["S A","S 2","S 3","S 4","S 5","S 6","S 7","D A","D 2","D 3","D 4","D 5","D 6","D 7","C A","C 2","C 3","C 4","C 5","C 6","C 7"]
data = {}
for i in range(3):
    data[i] = []
    for j in range(7):
        data[i].append(deck.pop(random.randint(0,len(deck)-1)))
print(data)
akhilsp
  • 1,063
  • 2
  • 13
  • 26
0

Randex! I did not quite get the idea of making 3 columns, as you asked in your question, but this is what you may find useful:

import random

deck = [
  "S A", "S 2", "S 3", "S 4", "S 5", "S 6", "S 7",
  "D A", "D 2", "D 3", "D 4", "D 5", "D 6", "D 7",
  "C A", "C 2", "C 3", "C 4", "C 5", "C 6", "C 7"
  ]

hands = {}

for stack in range(3):
  hands[stack] = []

You may also assign a randomly generated value to later on .append() and then remove from the list for more code readability

  for card in range(7):
    hands[stack].append(deck.pop(random.randint(0, len(deck) - 1)))

The "hands" will be assigned with a random card from the deck, until every "hand" has 7 cards stacked

for stack in hands:
  print(stack, '-', hands[stack])