将带有 str 的 int 转换为列表(尝试将整数打印为单词)
Converting int with str into list (trying to print integers as words)
我正在为我创建的游戏制作一副纸牌,并且 运行 遇到了重命名我的面部纸牌的问题。所有价值为 11-14 的卡片我想在代码中保留它们的价值,但向用户显示为 Jack、Queen、King 和 Ace(分别)。使用 str()
和 int()
函数不起作用。使用 print().format
无效。并且使用 match class 字典不起作用。我无法使映射工作。
相反,当我实例化我的洗牌后的牌组时,我得到了我的整副牌加上 4 张额外的牌(4 张 J、4 张 Q、4 张 K、4 张 A),虽然我没有通过 random.shuffle()
怀疑这是因为它们没有通过 self.cards.
相关代码如下:
import random
class Card(object):
def __init__(self, value, suit):
self.value = value
self.suit = suit
def show(self):
print('{} of {}'.format(self.value, self.suit))
class Deck(object):
def __init__(self):
self.cards = []
self.build()
def build(self):
for s in ["Joker"]:
for v in range(1, 3):
self.cards.append(Card(v, s))
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(2, 15):
if v is 11:
print('{} of {}'.format("Jack", s))
if v is 12:
print('{} of {}'.format("Queen", s))
if v is 13:
print('{} of {}'.format("King", s))
if v is 14:
print('{} of {}'.format("Ace", s))
self.cards.append(Card(v, s))
def show(self):
for c in self.cards:
c.show()
def shuffle(self):
for i in range(len(self.cards)-1, 0, -1):
random.shuffle(self.cards)
def draw(self):
return self.cards.pop()
if v is 11:
print('Jack of {}'.format(s))
else if v is 12:
print('Queen of {}'.format(s))
else if v is 13:
print('King of {}'.format(s))
else if v is 14:
print('Ace of {}'.format(s))
将相关部分改成这个
我想你想要的是一个__repr__
方法。如果您将以下方法添加到您的 Card 对象,当您调用 print(Deck().draw())
时,您将获得卡片名称而不是 <__main__.Card object at 0x00000000036359E8>
.
def __repr__(self):
return '{} of {}'.format(self.value, self.suit)
这是您要找的吗?
如您所料,人脸牌需要在Card.show()
方法中进行处理。
def show(self):
if self.value is 11:
print('Jack of {}'.format(self.suit))
elif self.value is 12:
print('Queen of {}'.format(self.suit))
elif self.value is 13:
print('King of {}'.format(self.suit))
elif self.value is 14:
print('Ace of {}'.format(self.suit))
else:
print('{} of {}'.format(self.value, self.suit))
请注意,您需要使用 self.value
检查 Card 对象是否为 Face 卡(A、J、Queen、K),并使用 self.suit
来确定 Card 的花色。
你也可以去掉Deck.build()
方法中的所有检查,这样就变成了:
def build(self):
for s in ["Joker"]:
for v in range(1, 3):
self.cards.append(Card(v, s))
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(2, 15):
self.cards.append(Card(v, s))
完成这些更改后,您将能够生成一副 52 张牌的牌组以及 2 个额外的小丑。我测试了其他 Deck 方法,经过这些更改后一切似乎都很好。
示例用法(python 2.7 打印函数):
if __name__ == '__main__':
d = Deck()
d.show()
print "number of cards=", len(d.cards)
d.shuffle()
c = d.draw()
c.show()
print "number of cards=", len(d.cards)
d.show()
我正在为我创建的游戏制作一副纸牌,并且 运行 遇到了重命名我的面部纸牌的问题。所有价值为 11-14 的卡片我想在代码中保留它们的价值,但向用户显示为 Jack、Queen、King 和 Ace(分别)。使用 str()
和 int()
函数不起作用。使用 print().format
无效。并且使用 match class 字典不起作用。我无法使映射工作。
相反,当我实例化我的洗牌后的牌组时,我得到了我的整副牌加上 4 张额外的牌(4 张 J、4 张 Q、4 张 K、4 张 A),虽然我没有通过 random.shuffle()
怀疑这是因为它们没有通过 self.cards.
相关代码如下:
import random
class Card(object):
def __init__(self, value, suit):
self.value = value
self.suit = suit
def show(self):
print('{} of {}'.format(self.value, self.suit))
class Deck(object):
def __init__(self):
self.cards = []
self.build()
def build(self):
for s in ["Joker"]:
for v in range(1, 3):
self.cards.append(Card(v, s))
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(2, 15):
if v is 11:
print('{} of {}'.format("Jack", s))
if v is 12:
print('{} of {}'.format("Queen", s))
if v is 13:
print('{} of {}'.format("King", s))
if v is 14:
print('{} of {}'.format("Ace", s))
self.cards.append(Card(v, s))
def show(self):
for c in self.cards:
c.show()
def shuffle(self):
for i in range(len(self.cards)-1, 0, -1):
random.shuffle(self.cards)
def draw(self):
return self.cards.pop()
if v is 11:
print('Jack of {}'.format(s))
else if v is 12:
print('Queen of {}'.format(s))
else if v is 13:
print('King of {}'.format(s))
else if v is 14:
print('Ace of {}'.format(s))
将相关部分改成这个
我想你想要的是一个__repr__
方法。如果您将以下方法添加到您的 Card 对象,当您调用 print(Deck().draw())
时,您将获得卡片名称而不是 <__main__.Card object at 0x00000000036359E8>
.
def __repr__(self):
return '{} of {}'.format(self.value, self.suit)
这是您要找的吗?
如您所料,人脸牌需要在Card.show()
方法中进行处理。
def show(self):
if self.value is 11:
print('Jack of {}'.format(self.suit))
elif self.value is 12:
print('Queen of {}'.format(self.suit))
elif self.value is 13:
print('King of {}'.format(self.suit))
elif self.value is 14:
print('Ace of {}'.format(self.suit))
else:
print('{} of {}'.format(self.value, self.suit))
请注意,您需要使用 self.value
检查 Card 对象是否为 Face 卡(A、J、Queen、K),并使用 self.suit
来确定 Card 的花色。
你也可以去掉Deck.build()
方法中的所有检查,这样就变成了:
def build(self):
for s in ["Joker"]:
for v in range(1, 3):
self.cards.append(Card(v, s))
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(2, 15):
self.cards.append(Card(v, s))
完成这些更改后,您将能够生成一副 52 张牌的牌组以及 2 个额外的小丑。我测试了其他 Deck 方法,经过这些更改后一切似乎都很好。
示例用法(python 2.7 打印函数):
if __name__ == '__main__':
d = Deck()
d.show()
print "number of cards=", len(d.cards)
d.shuffle()
c = d.draw()
c.show()
print "number of cards=", len(d.cards)
d.show()