擅长:python、mysql、java
<p>你已经非常非常接近你所拥有的</p>
<p>由于<code>values</code>和<code>suits</code>是Card类的属性,因此需要使用相同的<code>self.</code>符号来访问它们</p>
<p>因此,您的卡片类将如下所示(我更改的行上方的注释):</p>
<pre><code>class Card(object):
'''This class sets up a playing card'''
suits = {'d':'diamonds', 'h':'hearts', 's':'spades', 'c':'clubs'}
values = {1:'Ace',2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:10, 11:'Jack', 12:'Queen', 13:'King'}
def __init__ (self, value, suit):
self.value = value
self.suit = suit
def get_value(self):
# Access the values dictionary with self.values
return self.values[self.value]
def get_suit(self):
# Same thing for suits!
return self.suits[self.suit]
def __str__(self):
# Changed the accessor for suits to be complete
my_card = str(self.value) + ' of ' + str(self.suits[self.suit])
return my_card
</code></pre>
<p>或者,对于您的<code>__str__</code>,您可以</p>
<pre><code>def __str__(self):
return self.get_value() + ' of ' + self.get_suit()
</code></pre>