所以当我点击deal_hand时,我正在做一个纸牌游戏和这个游戏,它将从我的班级Card()处理一张5手牌(只是值并显示它们)。然后我想把它们平均并显示出来(除以5并显示)。我不知道该怎么做。这是Class卡:
import random
class Card:
def __init__(self):
self.value = 0
self.face_value = ''
def deal(self):
self.set_value(random.randint(1,13))
def set_value(self, value):
self.value = value
self.set_face_value()
def set_face_value(self):
faces = {1: "Ace", 2: "two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine", 10: "Ten", 11: "Jack", 12: "Queen", 13: "King"}
self.face_value = faces[self.value]
def __str__(self):
return self.face_value
main()的
我还没有完成其他功能,因为我不知道怎么做但是它是这样的:
def deal_hand():
card1 = Card()
card1.deal()
for i in range(5):
card1.deal()
print("Your 5 hand card is")
print(card1)
我无法让程序显示5张牌。如果这很难理解,我很抱歉,但程序假设显示:
The 5-card hand is:
Jack
Three
Queen
Two
Seven
我该怎么做?
答案 0 :(得分:0)
你的缩进不正确,试一试。
同时将print("Your 5 hand card is")
移出for循环。
import random
class Card:
def __init__(self):
self.value = 0
self.face_value = ''
def deal(self):
self.set_value(random.randint(1,13))
def set_value(self, value):
self.value = value
self.set_face_value()
def set_face_value(self):
faces = {1: "Ace", 2: "two", 3: "Three", 4: "Four",
5: "Five", 6: "Six", 7: "Seven", 8: "Eight",
9: "Nine", 10: "Ten", 11: "Jack", 12: "Queen",
13: "King"}
self.face_value = faces[self.value]
def __str__(self):
return self.face_value
def deal_hand():
card1 = Card()
card1.deal()
print("Your 5 hand card is")
for i in range(5):
card1.deal()
print(card1)
deal_hand()
输出:
Your 5 hand card is
Queen
King
Jack
Queen
Four
答案 1 :(得分:0)
另一种更好的方法是使用属性
import random
class Card:
def __init__(self):
self.value = 0
def shuffle(self):
self.value = random.randint(1, 13)
@property
def face_value(self):
faces = {1: "Ace", 2: "two", 3: "Three", 4: "Four",
5: "Five", 6: "Six", 7: "Seven", 8: "Eight",
9: "Nine", 10: "Ten", 11: "Jack", 12: "Queen",
13: "King"}
return faces[self.value]
def __str__(self):
return self.face_value
def deal_hand():
card = Card()
print("Your 5 hand card is")
for i in range(5):
card.shuffle()
print(card)
deal_hand()
输出:
Your 5 hand card is
Seven
Eight
Ace
Seven
Ace