SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
0.lists
Exercise:
Programming Excercises
Question:25 | ISBN:978013274719 | Edition: 6

Question

(Game: pick four cards) Write a program that picks four cards from a deck of 52 cards and computes their sum. An ace, king, queen, and jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yield the sum of 24.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program Code:

import random

# Create a list representing a deck of 52 cards
deck = list(range(1, 14)) * 4  # Each card is represented by a number from 1 to 13

# Initialize a counter to keep track of the number of picks that yield the sum of 24
count = 0

# Perform 10,000 iterations to pick four cards and compute their sum
for _ in range(10000):
    # Shuffle the deck
    random.shuffle(deck)
    
    # Pick four cards from the shuffled deck
    pickCards = deck[:4]
    
    # Compute the sum of the picked cards
    cardSum = sum(pickCards)
    
    # Check if the sum is equal to 24
    if cardSum == 24:
        count += 1

# Display the number of picks that yield the sum of 24
print("Number of picks that yield the sum of 24:", count)

 

Executed Output 1:

Number of picks that yield the sum of 24: 463

Executed Output 2:

Number of picks that yield the sum of 24: 452

 

0 0

Discussions

Post the discussion to improve the above solution.