在Python中打印出类的问题
我们应该使用下面的代码来打印其中列出的参数,但是目前我们无法这样做,并且正在使用一种循环方法。这应该是打印出来的东西,而不是我们在playturn函数的游戏类中打印出来的东西在Python中打印出类的问题,python,python-3.x,Python,Python 3.x,我们应该使用下面的代码来打印其中列出的参数,但是目前我们无法这样做,并且正在使用一种循环方法。这应该是打印出来的东西,而不是我们在playturn函数的游戏类中打印出来的东西 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]
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)
这是我们的实际代码,如果您还看到任何其他问题,我们将非常感谢您的输入
from random import*
#do we need to address anywhere that all face cards are worth 10?
class Card(object):
def __init__(self,suit,number):
self.number=number
self.suit=suit
def __str__(self):
return '%s'%(self.number)
class DeckofCards(object):
def __init__(self,deck):
self.deck=deck
self.shuffledeck=self.shuffle()
def shuffle(self):
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
def blackjacksearch(self):
if Game.gettot(self.player.hand)==21:#changed
return True
else:
return False
def firstround(self):
#self.player.inout=True#do we need this since this is above
#self.player.hand=[]#do wee need this....
#self.dealer.hand=[]#do we need this ....
self.player.hand.append(DeckofCards.deal(self.deck))
for card in self.player.hand:
a=card
print(self.player.name + ' ,you were dealt a '+str(a))
self.dealer.hand.append(DeckofCards.deal(self.deck))
for card in self.dealer.hand:
a=card
print('The Dealer has '+str(a))
playerbid=int(input(self.player.name + ' how much would you like to bet? '))
self.player.money-=playerbid
self.player.bid=playerbid
def playturn(self): #should this be changed to inout instead of hit.....we never use inout
#for player in self.player:
# a=player
#print(str(a))
hit=input('Would you like to hit? ') #should input be in loop?
while self.player.inout==True: #and self.blackjack!=True:#changed
print(self.player.name + ' , your hand has:' + str(self.player.hand)) #do we want to make this gettot? so it prints out the players total instead of a list....if we want it in a list we should print it with out brakets
self.player.hand.append(DeckofCards.deal(self.deck))
for card in self.player.hand:
a=card
print('The card that you just drew is: ' + str(a))
#print(Game.gettot(self.player.hand))
hit=input('Would you like to hit? ')
if hit=='yes':
(self.player.hand.append(DeckofCards.deal(self.deck)))#changed
self.player.inout==True#
else:
(self.player.hand) #changed
self.player.inout==False #changed
if self.player.blackjack==True:
print(self.player.name + " has blackjack ")
if hit=='no':
print (self.player.hand.gettot())
def playdealer(self):
while Game.gettot(self.dealer.hand)<17:#changed
self.dealer.hand.append(DeckofCards.deal(self.deck))
dealerhand=Game.gettot(self.dealer.hand) #changed
print(dealerhand)
if Game.gettot(self.dealer.hand)==21:#changed
self.dealer.blackhjack=True
dealerhand1=Game.gettot(self.dealer.hand)#changed
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'):
b=total+x #changed
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')]
def main():
x = input("Player's name? ")
blackjack = Game(ls,x)
blackjack.playgame()
main()
来自随机导入的*
#我们是否需要解决所有人脸卡都值10英镑的问题?
类别卡(对象):
定义初始(自我、套装、编号):
self.number=number
西服
定义(自我):
返回“%s%”(self.number)
卡片类别(对象):
def uuu init uuuu(自我,甲板):
self.deck=甲板
self.shuffledeck=self.shuffle()
def随机播放(自):
b=[]
计数=0
而count0:
返回(self.shuffledeck.pop(0))
其他:
洗牌(自我)
返回(self.shuffledeck.pop(0))
类播放器(对象):
定义初始(自我、姓名、手、输入、金钱、分数、出价):
self.name=name
手
self.inout=inout
钱=钱
self.score=分数
self.bid=bid
定义(自我):
x=self.name+“:\t”
x+=“卡:”
对于范围内的y(len(self.hand)):
x+=self.hand[y]。脸+self.hand[y]。西装+“”
如果(self.name!=“经销商”):
x+=“\t货币:$”+str(self.Money)
返回(x)
班级游戏(对象):
定义初始(自身、牌组、玩家):
self.player=player(player,[],True,100,0,0)
self.dealer=玩家(“dealer”,[],真,100,0,0)
自甲板=甲板(甲板)
self.blackjack=False
def blackjacksearch(自):
如果Game.getto(self.player.hand)==21:#更改
返回真值
其他:
返回错误
def第一轮(自我):
#self.player.inout=True#既然这个在上面,我们需要它吗
#self.player.hand=[]我们需要这个吗。。。。
#self.dealer.hand=[]我们需要这个吗。。。。
self.player.hand.append(DeckofCards.deal(self.deck))
对于self.player.hand中的卡:
a=卡
打印(self.player.name+),您收到了“+str(a))
self.dealer.hand.append(DeckofCards.deal(self.deck))
对于self.dealer.hand中的卡:
a=卡
打印('经销商有'+str(a))
playerbid=int(输入(self.player.name+“您想赌多少?”)
self.player.money-=玩家ID
self.player.bid=playerbid
def playturn(self):#是否应该将其改为inout而不是hit….我们从不使用inout
#对于self.player中的玩家:
#a=玩家
#印刷品(str(a))
hit=input('您想点击吗?')#输入是否应该在循环中?
而self.player.inout==True:#和self.blackjack=对:#已更改
打印(self.player.name+),你的手牌有:'+str(self.player.hand))#我们要做这个getto吗?所以它会打印出球员总数,而不是一份名单……如果我们想在名单上打印,我们应该打印出来,但不要打印brakets
self.player.hand.append(DeckofCards.deal(self.deck))
对于self.player.hand中的卡:
a=卡
打印('你刚才画的卡片是:'+str(a))
#打印(Game.getto(self.player.hand))
点击=输入('您想点击吗?')
如果命中==“是”:
(self.player.hand.append(DeckofCards.deal(self.deck)))#更改
self.player.inout==True#
其他:
(self.player.hand)#更改
self.player.inout==False#已更改
如果self.player.blackjack==真:
打印(self.player.name+“有21点”)
如果命中==‘否’:
打印(self.player.hand.getto())
def playdealer(自身):
游戏时。gettot(self.dealer.hand)21:
总数+=1
其他:
总数+=11
如果x==卡片('D','A'):
b=总计+x
如果b>21:
总数+=1
其他:
总数+=11
如果x==卡片('S','A'):
b=总计+x
如果b>21:
总数+=1
其他:
总数+=11
如果x==卡片('C','A'):
b=总计+x#已更改
如果b>21:
总数+=1
其他:
总数+=11
其他:
总数+=x
报税表(总数)
def游戏(自我):
play=“是”
while(play.lower()=“是”):
self.firstround()
self.playturn()
如果self.player.blackjack==True:
打印(self.player.name+“获得21点!”)
self.player.money+=self.player.bid*1.5
打印(self.player.name+“现在有”+str(self.player.money))
打印(“\n”)
self.player.inout=False
如果self.player.score>21:
打印(self.player.name+“丢失,总计”+str(self.player.score))
self.player.money-=self.player.bid
打印(self.player.name+“现在有”+str(self.player.money))
打印(“\n\n”)
self.player.inout=False
self.playdaler()
如果self.dealer.blackjack==真:
打印(“经销商获得21点,经销商获胜\n”)
self.player.money-=self.player.bid
打印(“圆形\n”)
打印(“\t”,self.dealer)
打印(“\t”,self.player)
打印(“\t经销商有”+str(self.Dealer.score)+“,“+self.player.name+”有”+str(self.player.score))
elif self.player.inout==真:
打印(“圆形\n”)
打印(“\t”,self.dealer)
打印(“\t”,self.player)
打印(“\n\t经销商有”+str(self.Dealer.sc
str(self.player.hand)
str(self.player)
print(self.player.name + ' , your hand has:' + str(self.player.hand))
print(self.player.name + ' , your hand has:' + [str(card) for card in self.player.hand])
print(self.player.name + ' , your hand has:' +
' '.join(str(card) for card in self.player.hand))
for y in range(len(self.hand)):
x +=self.hand[y].face + self.hand[y].suit + " "
for card in self.hand:
x += str(card) + " "
x += " ".join(str(card) for card in self.hand) + " "
def str_hand(hand):
return " ".join(str(card) for card in self.hand)