did card dealer

This commit is contained in:
EggMan 2024-04-23 20:55:10 -04:00
parent e49d51eb06
commit 08443c32d5
3 changed files with 71 additions and 2 deletions

View File

@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" /> <excludeFolder url="file://$MODULE_DIR$/.venv" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.11" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (CNSA-266-Personal)" project-jdk-type="Python SDK" /> <component name="Black">
<option name="sdkName" value="Python 3.11 (CNSA-266-Personal)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
</project> </project>

66
card_dealer.py Normal file
View File

@ -0,0 +1,66 @@
# card_dealer.py: main function
# This program uses a dictionary as a deck of cards.
def main():
# Create a deck of cards.
deck = create_deck()
# Get the number of cards to deal.
num_cards = int(input('How many cards should I deal? '))
# Deal the cards.
deal_cards(deck, num_cards)
# Line 5 calls the create_deck function. The function creates a dictionary with key-value pairs for a deck of cards, and it returns a reference to the dictionary. The reference is assigned to the deck variable.
# Line 8 prompts the user to enter the number of cards to deal. The input is converted to an integer and is assigned to the num_cards variable.
# The create_deck function returns a dictionary
# representing a deck of cards.
def create_deck():
# Create a dictionary with each card and its value
# stored as key-value pairs.
deck = {'Ace of Spades':1, '2 of Spades':2, '3 of Spades':3,
'4 of Spades':4, '5 of Spades':5, '6 of Spades':6,
'7 of Spades':7, '8 of Spades':8, '9 of Spades':9,
'10 of Spades':10, 'Jack of Spades':10,
'Queen of Spades': 10, 'King of Spades': 10,
'Ace of Hearts':1, '2 of Hearts':2, '3 of Hearts':3,
'4 of Hearts':4, '5 of Hearts':5, '6 of Hearts':6,
'7 of Hearts':7, '8 of Hearts':8, '9 of Hearts':9,
'10 of Hearts':10, 'Jack of Hearts':10,
'Queen of Hearts':10, 'King of Hearts': 10,
'Ace of Clubs':1, '2 of Clubs':2, '3 of Clubs':3,
'4 of Clubs':4, '5 of Clubs':5, '6 of Clubs':6,
'7 of Clubs':7, '8 of Clubs':8, '9 of Clubs':9,
'10 of Clubs':10, 'Jack of Clubs':10,
'Queen of Clubs': 10, 'King of Clubs': 10,
'Ace of Diamonds':1, '2 of Diamonds':2, '3 of Diamonds':3,
'4 of Diamonds':4, '5 of Diamonds':5, '6 of Diamonds':6,
'7 of Diamonds':7, '8 of Diamonds':8, '9 of Diamonds':9,
'10 of Diamonds':10, 'Jack of Diamonds':10,
'Queen of Diamonds': 10, 'King of Diamonds': 10}
# Return the deck.
return deck
# The deal_cards function deals a specified number of cards
# from the deck.
def deal_cards(deck, number):
# Initialize an accumulator for the hand value.
hand_value = 0
# Make sure the number of cards to deal is not
# greater than the number of cards in the deck.
if number > len(deck):
number = len(deck)
# Deal the cards and accumulate their values.
for count in range(number):
card, value = deck.popitem()
print(card)
hand_value += value
# Display the value of the hand.
print('Value of this hand:', hand_value)
# Call the main function.
main()