在下面的代码中,它首先工作,但是当我想告诉我经销商有什么卡号时,它会返回一些我不理解的加密内容。
[<__main__.Card object at 0x025A4E50>]
如何让它返回相应的卡号?
也有人说我可能需要在比较事物时使用__eq__
而不是我现在的方式可以解释原因,以及如何使用__eq__
?
下面是我试图开始工作的程序的代码,它是一个二十一点游戏。
from random import*
class Card(object):
def __init__(self,suit,number):
self.suit=suit
self.number=number
class DeckofCards(object):
def __init__(self,deck):
self.deck=deck
self.shuffledeck=self.shuffle()
#print(self.shuffledeck)
def shuffle(self):
#print('This is shuffle function')
b=[]
count=0
while count<len(self.deck):
a=randrange(0,len(self.deck))
if a not in b:
b.append(self.deck[a])
count+=1
return(b)
def deal(self):
if len(self.shuffledeck)>0:
return(self.shuffledeck.pop(0))
else:
shuffle(self)
return(self.shuffledeck.pop(0))
class Player(object):
def __init__(self,name,hand,inout,money,score,bid):
self.name=name
self.hand=hand
self.inout=inout
self.money=money
self.score=score
self.bid=bid
def __str__(self):
x = self.name + ":\t"
x += "Card(s):"
for y in range(len(self.hand)):
x +=self.hand[y].face + self.hand[y].suit + " "
if (self.name != "dealer"):
x += "\t Money: $" + str(self.money)
return(x)
class Game(object):
def __init__(self,deck, player):
self.player=Player(player,[],True,100,0,0)
self.dealer=Player("Dealer",[],True,100,0,0)
self.deck=DeckofCards(deck)
self.blackjack= False #self.blackjacksearch()
def blackjacksearch(self):#this is where it says there is an error we moved this because she said it needed to be in this class to get the Getot function
if self.player.hand.gettot()==21:
return True
else:
return False
def firstround(self):
self.player.inout=True
self.player.hand=[]
self.dealer.hand=[]
self.dealer.hand.append(DeckofCards.deal(self.deck))
print('The Dealer has '+str(self.dealer.hand))
playerbid=int(input('How much would you like to bet?'))
self.player.bid=playerbid
def playturn(self):
while self.player.blackjack!=True or hit=='yes':
print(self.player.hand)
a=self.player.hand.append(deal())
print('The card that you just drew is ' + str(a))
print(gettot())
hit=input('Would you like to hit? ')
if hit=='yes':
return(self.player.hand.append(deal()))
else:
return() #might need to change this
if self.player.blackjack==True:
print(self.player.name + " has blackjack ")
if hit=='no':
print (self.player.hand.gettot())
def playdealer(self):
while self.dealer.hand<17:
self.dealer.hand.append(deal())
dealerhand=self.dealer.hand.gettot() #confused
print(dealerhand)
if self.dealer.hand==21:
self.dealer.blackhjack=True
dealerhand1=self.dealer.hand.gettot()
print(dealerhand1)
def gettot(self,hand):
total=0
for x in self.hand:
if x==Card('H','A'):
b=total+x
if b>21:
total+=1
else:
total+=11
if x==Card('D','A'):
b=total+x
if b>21:
total+=1
else:
total+=11
if x==Card('S','A'):
b=total+x
if b>21:
total+=1
else:
total+=11
if x==Card('C','A'):
if b>21:
total+=1
else:
total+=11
else:
total+=x
return(total)
def playgame(self):
play = "yes"
while (play.lower() == "yes"):
self.firstround()
self.playturn()
if self.player.blackjack == True:
print(self.player.name + " got BLACKJACK! ")
self.player.money += self.player.bid * 1.5
print (self.player.name + " now has " + str(self.player.money))
print("\n")
self.player.inout = False
if self.player.score > 21:
print(self.player.name + " lost with a tot of " + str(self.player.score))
self.player.money -= self.player.bid
print (self.player.name + " now has " + str(self.player.money))
print ("\n\n")
self.player.inout = False
self.playdealer()
if self.dealer.blackjack == True:
print("Dealer got blackjack, dealer wins\n")
self.player.money -= self.player.bid
print("Round\n")
print("\t",self.dealer)
print("\t",self.player)
print("\t Dealer has " + str(self.dealer.score) + ", " + self.player.name + " has " + str(self.player.score))
elif self.player.inout == True:
print("Round\n")
print("\t",self.dealer)
print("\t",self.player)
print("\n\t Dealer has " + str(self.dealer.score) + ", " + self.player.name + " has " + str(self.player.score))
if self.dealer.score > 21:
print("\t Dealer lost with a total of " + str(self.dealer.score))
self.player.money += self.player.bid
print(self.player.name + " now has " + str(self.player.money))
elif self.player.score > self.dealer.score:
print("\t" +self.player.name + " won with a total of " + str(self.player.score))
self.player.money += self.player.bid
print("\t"+self.player.name + " now has " + str(self.player.money))
else:
print("\t Dealer won with a total of " + str(self.dealer.score))
self.player.money -= self.player.bid
print("\t"+self.player.name + " now has " + str(self.player.money))
else:
print("Round")
print("\t",self.dealer)
print("\t",self.player)
if self.player.blackjack == False:
print("\t "+ self.player.name + " lost" )
else:
print("\t "+self.player.name + " Won!")
if self.player.money <= 0:
print(self.player.name + " out of money - out of game ")
play = "no"
else:
play = input("\nAnother round? ")
print("\n\n")
print("\nGame over. ")
print(self.player.name + " ended with " + str(self.player.money) + " dollars.\n")
print("Thanks for playing. Come back soon!")
ls= [Card('H','A'),Card('H','2'),Card('H','3'),Card('H','4'),Card('H','5'),Card('H','6'),Card('H','7'),Card('H','8'),Card('H','9'),Card('H','10'),
Card('H','J'),Card('H','Q'),Card('H','K'),
Card('S','A'),Card('S','2'),Card('S','3'),Card('S','4'),Card('S','5'),
Card('S','6'),Card('S','7'),Card('S','8'),Card('S','9'),Card('S','10'),
Card('S','J'),Card('S','Q'),Card('S','K'),
Card('C','A'),Card('C','2'),Card('C','3'),Card('C','4'),Card('C','5'),
Card('C','6'),Card('C','7'),Card('C','8'),Card('C','9'),Card('C','10'),
Card('C','J'),Card('C','Q'),Card('C','K'),
Card('D','A'),Card('D','2'),Card('D','3'),Card('D','4'),Card('D','5'),
Card('D','6'),Card('D','7'),Card('D','8'),Card('D','9'),Card('D','10'),
Card('D','J'),Card('D','Q'),Card('D','K')]
'''tom=Card('Heart','Queen')
print(tom.suit)
print(DeckofCards(ls))
print(ls.suit)'''
def main():
x = input("Player's name? ")
blackjack = Game(ls,x)
blackjack.playgame()
main()
答案 0 :(得分:1)
打印卡片而不是卡片对象列表,并为卡片提供一个不错的__str__
方法,使其显示为人类可读的。而不是
print(self.player.hand)
试试这个:
for card in self.player.hand:
print(card)
并在class Card
插入此内容:
def __str__(self):
return '%s%s' % (self.number, self.suit)
关于__eq__
的问题:只要使用__eq__()
运算符对相等性进行比较,就会调用对象==
方法。如果没有这样的方法,如果它们是同一个对象,则两个对象仅相等。所以在你的卡片中你可能想要这样:
def __eq__(self, other):
return self.number == other.number and self.suit == other.suit
答案 1 :(得分:0)
在str()
对象上使用Card
时,它会调用继承的__str__
方法,该方法将打印出对象的类名和地址。你称之为加密的是对象的地址。如果要打印出不同的内容,请覆盖班级中的__str__
方法。例如:
class Card(object):
def __str__(self):
return "Card " + self.suit + " " + self.number