Happy late New Year, and welcome back after winter break! :)
RESOURCES:
Class slides: https://docs.google.com/presentation/d/16-34_-argpSG1VIpp5Xvm-h-cjjuugiq_whAsP6jgG0/edit?usp=sharing
Example code: https://replit.com/@ShravyaS/IntroToPython-14
HOMEWORK / MINI-PROJECT:
Create a program that simulates a standard deck of cards without Jokers. There are 4 suits of cards (Clubs, Diamonds, Hearts, Spades), each with 13 cards: Jack, Queen, King, Ace, and the numbers 2 through 10. This makes for a total of 4*13 = 52 cards.
Your program should also keep track of one person's hand as well as a deck of cards, and the sum of the cards in the deck and that person's hand should of course be 52.
Allow the user to draw cards into the person's hand, shuffle the deck, print out the cards in deck or in hand, etc. through commands entered through input.
(Implement a draw function with a number of cards to draw, a shuffle function, printing for the hand/deck.)
Enhancement ideas: keep track of 4 (or more) players' hands, print out the cards as little ASCII drawings, implement a simple card game with these functions.
Write your answer as a comment, and feel free to email the TAs (Shravya and Eric) if you're stuck. See you next class!
import random deck = ["C_Jack", "C_Queen", "C_King", "C_Ace", "C_2", "C_3", "C_4", "C_5", "C_6", "C_7", "C_8", "C_9", "C_10", \ "D_Jack", "D_Queen", "D_King", "D_Ace", "D_2", "D_3", "D_4", "D_5", "D_6", "D_7", "D_8", "D_9", "D_10", \ "H_Jack", "H_Queen", "H_King", "H_Ace", "H_2", "H_3", "H_4", "H_5", "H_6", "H_7", "H_8", "H_9", "H_10", \ "S_Jack", "S_Queen", "S_King", "S_Ace", "S_2", "S_3", "S_4", "S_5", "S_6", "S_7", "S_8", "S_9", "S_10"]; hand = []; def shuffle(list_in): list_out = random.choices(list_in, k = len(list_in)); return list_out; print("Original deck"); print(deck); deck = shuffle(deck); print("\nDeck after shuffle"); print(deck); print('\nWhat is your choice, "Draw", "Shuffle", "Deck", "Hand" or "Stop"?'); user_input = input(); while user_input.lower() != "stop": if user_input.lower() == "draw": print('How many cards do you want to draw?') n = int(input()); my_draw = []; if n <= 0 or n > len(deck): print(f'Your choice {n} is out of range, default to one'); n = 1; while n > 0: my_draw.append(deck.pop(0)); n -= 1; hand = hand + my_draw; print(f'\nYou drew {my_draw}'); elif user_input.lower() == "shuffle": print("\nOriginal deck"); print(deck); deck = shuffle(deck); print("\nDeck after shuffle"); print(deck); elif user_input.lower() == "hand": print('\nHere is what you have in your hand') print(hand); elif user_input.lower() == "deck": print('\nHere is what the deck has') print(deck); print('\nWhat is your choice, "Draw", "Shuffle", "Deck", "Hand" or "Stop"?'); user_input = input();
Solution
import random deck = ['CJ', 'CQ', 'CK', 'CA', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'DJ', 'DQ', 'DK', 'DA', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'D10', 'HJ', 'HQ', 'HK', 'HA', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'H10', 'SJ', 'SQ', 'SK', 'SA', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10'] hand = [] handnum = int(input("How many cards do you want in your hand?")) if handnum > 52: print("oops") print("The number you entered is bigger than 52") for x in range(handnum): z=random.randint(0,(len(deck)-1)) hand.append(deck[z]) deck.remove(deck[z]) print("Cards in hand:", hand) print("Cards in deck:", deck) while True: action = input("What would you like to do next? Draw card, shuffle deck, return card, r(repeat), exit\n") if action != "r" or "repeat": prevaction = action if action.lower() == "r" or "repeat": try: action = prevaction print("hi") except: print("oops") pass if action.lower() == "draw card": a = random.randint(0,(len(deck)-1)) hand.append(deck[a]) deck.remove(deck[a]) print(hand) elif action.lower() == "shuffle deck": random.shuffle(deck) print(deck) elif action.lower() == "exit": print("Thank you for playing") break elif action.lower() == "return card": print("Which card do you want to return from your hand? Card, random, all", hand) returncard = input() if returncard.lower() == "random": b = random.randint(0,(len(hand)-1)) deck.append(hand[b]) hand.remove(hand[b]) print("Card removed:", hand[b]) elif returncard.lower() == "all": deck.extend(hand) hand.clear() print(deck) else: try: deck.append(returncard.upper()) hand.remove(returncard.upper()) print(hand) except: print("Oops") else: print("Try again") pass