Python Challenge 5

Python Challenge 5

The code below uses a loop within a loop to iterate through all four suits in a pack of cards (hearts, clubs etc). Then for each suit it will assign a value to a card (Ace,2,3,4-King). The program will then put the cards into lists for each suit, as well as adding the card to the deck. Finally it will then print out all cards in each suit, the entire deck and a shuffled deck.

 

The challenge is can you develop this code into a card game that could allow two player to play snap?

Are there any other card games you could develop by building onto this code?

 

 

deck=[]
shuffled=[]
table=[]
suits = ["diamonds", "spades", "hearts", "clubs"];
values = ("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K")
diamonds=[]
hearts=[]
clubs=[]
spades=[]

import random

 

def deal():
    for i in suits:
        for j in values:
            deck.append([j,i])
            if i==suits[0]:
                diamonds.append([j,i])
            elif i==suits[1]:
                spades.append([j,i])
            elif i==suits[2]:
                hearts.append([j,i])
            else:
                clubs.append([j,i])
    
    print("Diamonds:",diamonds)
    print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
    print("Hearts:",hearts)
    print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
    print("Clubs:",clubs)
    print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
    print("Spades:",spades)
    print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
    print("Full deck:",deck)
    print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
    random.shuffle(deck)
    print("Shuffled deck:",deck)

deal()