cardpicker.py

Created by matt-numworks

Created on May 07, 2025

1.11 KB

This script allows the user to randomly draw a card from a deck of cards. Once a card is chosen, it is removed from the deck and you can continue drawing cards until it runs out.


import random

# Create a standard deck of 52 playing cards
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
deck = []

# Manually populate the deck instead of using a list comprehension
for suit in suits:
    for rank in ranks:
        deck.append(rank + " of " + suit)
        
def draw_card():
    if deck:
        card = random.choice(deck)  # Select a random card
        deck.remove(card)  # Remove the selected card from the deck
        return card
    else:
        return None  # Return None when the deck is empty

while deck:
    print("Press Enter to draw a card...")  # Prompting message
    print("Input exit to quit simulator")
    _ = input()  # Waits for user input (NumWorks may require adjustments)
    
    if _.lower() == 'exit':
            print("Exiting the script. Goodbye!")
            break
          
    drawn_card = draw_card()
    
    if drawn_card:
        print("You drew:", drawn_card)
    else:
        print("The deck is empty! No more cards to draw.")
        break

print("Game over! Thanks for playing.")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.