我是python的新手,正在编写基本的扑克游戏。我要做的第一件事是打印52张卡片组。我已经为面值和西服创建了列表。我设置了几个循环以遍历面部和衣服,以创建52张独特的卡片。问题是在打印牌组时,例如黑桃A将打印为“ A”,“ s”,但看起来并不好。我希望它看起来像As。因此,如何将一个列表索引中的值放在另一个列表索引的旁边,以便它们在彼此相邻的位置打印呢? 还是我用错误的构造解决了这个问题?我想知道是否为具有面值和花色的扑克牌创建数据类型。
faces = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
suits = ["s", "h", "d", "c"]
deck = []
for i in range (0, len(faces)):
for j in range (0, len(suits)):
card = (faces[i], suits[j])
deck.append(card)
print(deck)
输出: [('A','s'),('A','h'),('A','d'),('A','c'),('2','s') ,('2','h'),('2','d'),('2','c'),('3','s'),('3','h') ,('3','d'),('3 ','c'),('4','s'),('4','h'),('4','d'),('4','c'),('5 ','s'),('5','h'),('5','d'),('5','c'),('6','s'),('6 ','h'),('6',' d'),('6','c'),('7','s'),('7','h'),('7','d'),('7',' c'),('8','s'),('8','h'),('8','d'),('8','c'),('9',' s'),('9','h'), ('9','d'),('9','c'),('10','s'),('10','h'),('10','d'), ('10','c'),('J','s'),('J','h'),('J','d'),('J','c'), (“ Q”,“ s”), ('Q','h'),('Q','d'),('Q','c'),('K','s'),('K','h'), ('K','d'),('K','c')]
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以只添加两个字符串。 这将打印并保存在所需的表格中。
for i in range (0, len(faces)):
for j in range (0, len(suits)):
card = faces[i]+suits[j]
deck.append(card)
print(deck)
或者您可以保存旧格式并在另一个循环中添加字符串以进行打印,如下所示。这样,您既可以将其存储在具有两个属性的数据类型中,又可以在打印时添加它们。
for d in deck:
print(d[0]+d[1],end=' ')
答案 2 :(得分:0)
只需将“ ,”更改为“ +”
const Op = require('./models').Sequelize.Op;
const dbresp = await Table.findAll({
attributes: [...],
where: {
...
createdAt: {
[Op.between]: [startDate, endDate],
},
},
logging: console.log,
raw: true,
order: [['createdAt', 'ASC']],
// limit: count,
});
答案 3 :(得分:0)
如果您读过Luciano Ramalho的书“ Fluent python”,您会希望他实现一副纸牌:
import collections
card = collections.namedtuple('Card', ['rank', 'suit'])
class FrenchDeck:
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits = 'spades diamonds clubs hearts'.split()
def __init__(self):
self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]
def __len__(self):
return len(self._cards)
def __getitem__(self, position):
return self._cards[position]
deck = FrenchDeck()
现在,您有了一个包含所有卡片信息的卡组。您可以随机选择任何想要的卡
from random import choice
print(choice(deck))
# may print Card(rank='3', suit='hearts')