对于我的编程类介绍,我需要创建一个程序,随机地随机洗牌,然后输出等级&在4x13网格中套装(显示为unicode符号)。我到目前为止的内容如下。如何让它随机输出?它目前按等级和套装顺序输出卡片。如何在4x13网格中输出?它目前以13x4网格输出。
以下是我的输出假设的示例: example output
(对于课程,我的教授希望我们列出单独的元组和嵌套序列,这就是为什么它们都在那里,对不起,如果它使代码显得凌乱)
import random
#Cards
SUITS = "\u2663","\u2665","\u2666","\u2660"
PIPS = "A","2","3","4","5","6","7","8","9","10","J","Q","K"
deck = [("A","\u2663"),("2","\u2663"),("3","\u2663"),("4","\u2663"),
("5","\u2663"),("6","\u2663"),("7","\u2663"),("8","\u2663"),("9","\u2663"),
("10","\u2663"),("J","\u2663"),("Q","\u2663"),("K","\u2663"),("A","\u2665"),
("2","\u2665"),("3","\u2665"),("4","\u2665"),("5","\u2665"),("6","\u2665"),
("7","\u2665"),("8","\u2665"),("9","\u2665"),("10","\u2665"),("J","\u2665"),
("Q","\u2665"),("K","\u2665"),("A","\u2666"),("2","\u2666"),("3","\u2666"),
("4","\u2666"),("5","\u2666"),("6","\u2666"),("7","\u2666"),("8","\u2666"),
("9","\u2666"),("10","\u2666"),("J","\u2666"),("Q","\u2666"),("K","\u2666"),
("A","\u2660"),("2","\u2660"),("3","\u2660"),("4","\u2660"),("5","\u2660"),
("6","\u2660"),("7","\u2660"),("8","\u2660"),("9","\u2660"),("10","\u2660"),
("J","\u2660"),("Q","\u2660"),("K","\u2660")]
#Retrieve random card
def deal_card():
for suit in SUITS:
for pip in PIPS:
print(suit + pip,end=" ")
print()
#Main Portion
deal_card()
答案 0 :(得分:2)
from itertools import product
from random import shuffle
SUITS = ["\u2663","\u2665","\u2666","\u2660"]
PIPS = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
deck = list(product(PIPS, SUITS))
shuffle(deck)
然后输入您的打印逻辑。这是一个相当懒惰的打印方法,可以完成示例链接显示的内容:
for i in range(0, len(deck), 4):
print("{} {} {} {}".format(deck[i][0]+deck[i][1],deck[i+1][0]+deck[i+1][1],deck[i+2][0]+deck[i+2][1],deck[i+3][0]+deck[i+3][1]))