2

Im trying to figure out how to get the program to recognize what card was drawn and remove it from the deck so it cant be drawn again. I know I could have don't a list of 52 where I have each item be a card name and pull it from that list and put it in a new list but with the way I did this that doesn't seem possible. so how can I make it so it only draws a card one time?

import random
import time

played = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0]

def loop():
keepLooping = True
while(keepLooping):
    global played
    print ('The player with the lower card goes first')
    print(' ')

    # player One draw
    draw = input("Player one would you like to draw?(y,n): ")
    if draw == 'y':
                 CardNumber = random.randint(2,14)
                 Num2 = random.randint(2,4)
                 Royal = {11: "Jack",12: "Queen",13: "King",14: "Ace"}
                 cardnum1 = Royal.get(CardNumber, CardNumber)
                 suits = {1: "Spades", 2: "Hearts", 3: "Diamonds", 4: "Clubs"}
                 cardnum2 = suits[Num2]
                 DrawOne = [cardnum1, cardnum2]
                 print(DrawOne)
                 print(' ')
    if draw == 'n':
        print ('ok')

    # player two draw
    draw = input("Player Two would you like to draw?(y,n): ")
    if draw == 'y':
                 CardNumber2 = random.randint(2,13)
                 Num3 = random.randint(2,4)
                 Royal = {11: "Jack",12: "Queen",13: "King",14: "Ace"}
                 cardnum3 = Royal.get(CardNumber2, CardNumber2)
                 suits = {1: "Spades", 2: "Hearts", 3: "Diamonds", 4: "Clubs"}
                 cardnum4 = suits[Num3]
                 DrawTwo = (cardnum3, cardnum4)
                 print(DrawTwo)
    if draw == 'n':
        print ('Then you lose')

    # Win/lose/tie   
    if CardNumber == CardNumber2:
        time.sleep(1)
        print(' ')
        print("it was a tie, lets re-draw")
        print(' ')
        keepLooping = True
    else:
        if CardNumber < CardNumber2:
            keepLooping = False
            time.sleep(.5)
            print(' ')
            print (DrawOne, 'Is the lower card, player 1 youre going first')
        if CardNumber > CardNumber2:
            keepLooping = False
            time.sleep(.5)
            print(' ')
            print (DrawTwo, 'Is the lower card, player 2 youre going first') 

    loop()
J WALKAZ
  • 61
  • 5

1 Answers1

1

Easy way conceptually (and not using numpy):

>>> cards = list(range(52))
>>> random.shuffle(cards)
>>> cards
[7, 28, 1, 49, 27, 36, 26, 16, 32, 23, 45, 19, 31, 13, 44, 5, 37, 3, 39, 29, 42, 11, 46, 6, 2, 0, 15, 14, 48, 38, 9, 51, 10, 20, 43, 25, 18, 12, 8, 21, 47, 4, 33, 24, 41, 50, 35, 17, 40, 22, 34, 30]

Now everytime you draw, you can pop one from the list.

>>> card = cards.pop()
>>> card
30

Then to get the suit and the number, you can do this:

suit = card % 4        # e.g. 0 is hearts, etc. (arbitrary)
number = card % 13 + 1 # so that 1 is 1, and 11 is Jack

In case modulo (%) is not familiar, please have a look here: How does % work in Python?

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • to make sure I understand are you saying replace this? `CardNumber = random.randint(2,14) Num2 = random.randint(2,4) Royal = {11: "Jack",12: "Queen",13: "King",14: "Ace"} cardnum1 = Royal.get(CardNumber, CardNumber) suits = {1: "Spades", 2: "Hearts", 3: "Diamonds", 4: "Clubs"}` – J WALKAZ Nov 13 '18 at 23:31
  • I'm sure you can figure it out using this. Final clue: `suit` will be a number, so you still need to convert it indeed using your "suits" dictionary, these parts didn't change. – PascalVKooten Nov 13 '18 at 23:33