M代码可以工作,但是我希望它使用Ace Jack Queen和King而不是1,11,12,13,但是我不知道如何更改我的代码,所以它会做到。
# imports random
import itertools, random
# make a deck of cards
deck = list(itertools.product(range(1, 14), ['Spade', 'Heart',
'Diamond', 'Club']))
# shuffles the deck
random.shuffle(deck)
# draw five cards
howmany = int(input('How many cards do you want to display? '))
print('You got:')
for i in range(howmany):
print(deck[i][0], 'of', deck[i][1])
当前结果。
您要显示多少张卡? 4 你得到了: 钻石11 俱乐部7 6颗钻石 钻石八颗
例如,我希望它为第一个输出
钻石杰克
答案 0 :(得分:0)
这就是我要做的。注意,我主要更改了代码的最后一部分。这是为了提供元组解包get
dictionary method和f-strings的用例:
# imports random
import itertools, random
# make a deck of cards
deck = list(itertools.product(range(1, 14), ['Spade', 'Heart',
'Diamond', 'Club']))
# shuffles the deck
random.shuffle(deck)
# have a dict of named cards
cards = {11: "Jack", 12: "Queen", 13: "King", 1:"Ace"}
# draw five cards
howmany = int(input('How many cards do you want to display? '))
print('You got:')
for i in range(howmany):
card, suit = deck[i] # unpack the tuple to have cleaner code
print(f"{cards.get(card, card)} of {suit}") # use f-string together with get
答案 1 :(得分:0)
import itertools, random
deck = list(itertools.product(['Ace']+ list(range(2,11)) + ['jack','Queen','King'] ,['Spade','Heart','Diamond','Club']))
random.shuffle(deck)
print("You got:")
for i in range(1):
print(deck[i][0], "of", deck[i][1])