計算機科学のブログ

ほしい物リスト

Python - Why Python: Similar but Different - random module, choice method

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter 1.(Why Python?: Similar but Different)、EXERCISE(83/682)の解答を求めてみる。

Jupyter(コード、入出力結果)

Cards.ipynb

import random

suits = ['Clubs', 'Spades', 'Hearts', 'Diamonds']
faces = ['Jack', 'Queen', 'King', 'Ace']
numbered = list(range(2, 11))
suits
['Clubs', 'Spades', 'Hearts', 'Diamonds']
def draw():
    the_suit = random.choice(suits)
    the_card = random.choice(faces + numbered)
    return the_card, 'of', the_suit
draw()
(10, 'of', 'Diamonds')