import random suits = ["Spades", "Hearts", "Diamonds", "Clubs"] values = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] def makeDeck(): """ Returns an unshuffled complete deck of cards, where each card is a tuple, (suit, value)""" deck = [] for s in suits: for v in values: deck.append((s, v)) return deck unshuffledDeck = makeDeck() def shuffle(deck): """ Returns a new, shuffled deck of cards from the given deck of cards""" sDeck = deck[:] random.shuffle(sDeck) return sDeck shuffledDeck = shuffle(unshuffledDeck) def pick(deck): """ Non-destructively pick and return a random card from the deck""" return random.choice(deck) def probOr(suit, value, deck, n): """ Returns the probability of choosing a card of the given suit or the given value from the given deck. n trials are run and averaged.""" yes = 0.0 for i in range(n): c = pick(deck) if (c[0] == suit) or (c[1] == value): yes += 1.0 return yes/n def probAnd(suit, value, deck, n): """ Returns the probability of choosing a card of the given suit and the given value from the given deck. n trials are run and averaged.""" yes = 0.0 for i in range(n): c = pick(deck) if (c[0] == suit) and (c[1] == value): yes += 1.0 return yes/n def probCond(suit, value, deck, n): """ Returns the probability of choosing a card of the given suit if card of the given value was chosen. n trials are run and averaged.""" yes = 0.0 total = 0 for i in range(n): c = pick(deck) if c[1] == value: total += 1 if c[0] == suit: yes += 1.0 return yes/total