Search code examples
pythonpython-3.xrandomplaying-cards

How to make cards work in python


This is my code so far:

import sys
import os
import random
Question():
    os.system('cls')
    SQ=input('Do you want to play blackjack y/n')
    if(SQ == y or SQ == Y):
        StartGame()
    if(SQ == n or SQ == N):
        sys.exit()
    if(SQ != n and SQ != N and SQ != y and SQ != Y):
        print('You did answer the question with a y or a n which correspond to yes and no accordingly')
        Question()
Question()

StartGame():
    slot1=False
    slot2=False
    slot3=False
    slot4=False
    slot5=False
    slot6=False
    slot7=False
    slot8=False
    slot9=False
    slot10=False
    slot11=False
    slot12=False
    slot13=False
    slot14=False
    slot15=False
    slot16=False
    slot17=False
    slot18=False
    slot19=False
    slot20=False
    slot21=False
    slot22=False
    slot22=False
    slot23=False
    Slot24=False
    slot25=False
    slot26=False
    slot27=False
    Slot28=False
    slot29=False
    slot30=False
    slot31=False
    slot32=False
    slot33=False
    slot34=False
    slot35=False
    slot36=False
    slot37=False
    slot38=False
    slot39=False
    slot40=False
    slot41=False
    slot42=False
    slot43=False
    slot44=False
    slot45=False
    slot46=False
    slot47=False
    slot48=False
    slot49=False
    slot50=False
    slot51=False
    slot52=False

    aceHEART = randrange(1, 52)
    aceHEART

I don't understand the correct way to make the slots and a random number generator together to make a random shuffle. How can i make it so it does not try to put more than one card in one slot. I also do not know how to manage these cards in a more efficient way. I am making a blackjack game in python and i do not know the correct way to approach this. Please help me in the best way you can.


Solution

  • Not sure what you're trying to do, but here is a way to generate a shuffled deck of cards:

    ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
    suite = ['clubs', 'hearts', 'spades', 'diamonds']
    
    deck = [r + ' ' + s for r in ranks for s in suite]
    
    random.shuffle(deck)
    

    Or with objects:

    class Card(object):
        def __init__(self, rank, suite):
            self.rank = rank
            self.suite = suite
    
    deck = [Card(r,s) for r in ranks for s in suite]
    random.shuffle(deck)