Trouble with printing out of Classes in Python -


we supposed use code below print out parameters listed in it, unable , using round method. supposed print out things instead of print out in game class in playturn function

 def __str__(self):         x = self.name + ":\t"         x += "card(s):"         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) 

here our actual code, if see other issues input appreciated

from random import* #do need address anywhere face cards 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 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):"         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 need since above         #self.player.hand=[]#do wee need this....         #self.dealer.hand=[]#do need ....         self.player.hand.append(deckofcards.deal(self.deck))         card in self.player.hand:             a=card         print(self.player.name + ' ,you dealt '+str(a))         self.dealer.hand.append(deckofcards.deal(self.deck))         card in self.dealer.hand:             a=card         print('the dealer has '+str(a))         playerbid=int(input(self.player.name + ' how bet? '))         self.player.money-=playerbid         self.player.bid=playerbid     def playturn(self): #should changed inout instead of hit.....we never use inout         #for player in self.player:         #    a=player         #print(str(a))         hit=input('would hit? ') #should input in loop?         while self.player.inout==true: #and self.blackjack!=true:#changed             print(self.player.name + ' , hand has:' + str(self.player.hand)) #do want make gettot? prints out players total instead of list....if want in list should print out brakets             self.player.hand.append(deckofcards.deal(self.deck))             card in self.player.hand:                 a=card             print('the card drew is: ' + str(a))             #print(game.gettot(self.player.hand))              hit=input('would 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         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 + " has " + str(self.player.money))                 print("\n")                 self.player.inout = false             if self.player.score > 21:                 print(self.player.name + " lost tot of " + str(self.player.score))                 self.player.money -= self.player.bid                 print (self.player.name + " 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 total of " + str(self.dealer.score))                     self.player.money += self.player.bid                     print(self.player.name + " has " + str(self.player.money))                 elif self.player.score > self.dealer.score:                     print("\t" +self.player.name + " won total of " + str(self.player.score))                     self.player.money += self.player.bid                     print("\t"+self.player.name + " has " + str(self.player.money))                 else:                     print("\t dealer won total of " + str(self.dealer.score))                     self.player.money -= self.player.bid                     print("\t"+self.player.name + " 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 " + str(self.player.money) + " dollars.\n")         print("thanks playing.  come 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() 

the problem that, in @ least places, you're trying print list.

while printing anything, including list, calls str on it, list.__str__ method calls repr on elements. (if don't know difference between str , rep, see difference between __str__ , __repr__ in python.)

if want print str of every element in list, have explicitly, map or list comprehension.

for example, instead of this:

print(self.player.name + ' , hand has:' + str(self.player.hand)) 

… this:

print(self.player.name + ' , hand has:' + [str(card) card in self.player.hand]) 

but still not want. ['8', '9'] instead of [<__main__.card object @ 0x1007aaad0>, <__main__.card object @ 0x1007aaaf0>], wanted more `8h 9c'. that, you'd want like:

print(self.player.name + ' , hand has:' +        ' '.join(str(card) card in self.player.hand)) 

you have similar (although more verbose) code inside player.__str__:

for y in range(len(self.hand)):     x +=self.hand[y].face + self.hand[y].suit + " " 

this code improved in few ways.

first, it's going raise attributeerror because you're using face instead of number. really, shouldn't need @ all—the whole reason created card.__str__ method can use str(card), right?

second, never want loop on range(len(foo)), if foo[y] inside loop. loop on foo directly.

putting together:

for card in self.hand:     x += str(card) + " " 

at rate, need same thing in both places.

the version uses join method , generator expression little simpler explicit loop, require bit more python knowledge understand. here's how you'd use here:

x += " ".join(str(card) card in self.hand) + " " 

your next problem card.__str__ doesn't include suit. so, instead of 8h 9c, you're going 8 9. should easy fix on own.


meanwhile, if find writing same code more once, want abstract out. could write function takes hand list , turns string:

def str_hand(hand):     return " ".join(str(card) card in self.hand) 

but might better create hand class wraps list of cards, , pass around, instead of using list directly.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -