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:
.selections
Exercise:
Programming Excercises
Question:24 | ISBN:978013274719 | Edition: 6

Question

(Game: pick a card ) Write a program that simulates picking a card from a deck of 52 cards. Your program should display the rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) and suit (Clubs, Diamonds, Hearts, Spades) of the card.


Here is a sample run of the program:

The card you picked is the Jack of Hearts

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Game: pick a card  Program Code:

import random

# Define the ranks and suits of a deck of cards
ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']

# Randomly select a rank and suit for the card
rank = random.choice(ranks)
suit = random.choice(suits)

# Display the picked card
print("The card you picked is the", rank, "of", suit)

Executed Output 1:

The card you picked is the Queen of Clubs

Executed Output 2:

The card you picked is the 10 of Hearts

 

0 0

Discussions

Post the discussion to improve the above solution.