Trouble with method inside of class in python -
we having trouble getting gettot() method work in side of our game class. when called inside of our game whole comes error saying dont have correct amount of inputs function operate correctly. first iteration of functions commented out further in project without stopping our game every time runs.
this gettot method having issues with:
the purpose of method total of cards player or dealer has in hand, if there ace, decides whether or not should 1 or 11 depending on gets closer 21 total points without going over.
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) 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 %s'%(self.number,self.suit) 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.deck) #need refill deck 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(self.player.name + ' , hand has:' + str([str(card) card in self.player.hand])) #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()
tl;dr 1 thing noticed this: x==card('h','a').
this won’t work unless define card type handle equality comparisons in useful way. default check if both same objects, , create new card, won’t same object x.
class card(object): # ... def __eq__ (self, other): return self.number == other.number , self.suit == other.suit also, this: b=total+x. if x card object, how imagine being added number? have define this, or b = total + x.number instead.
another thing define gettot take hand parameter, in function, iterate on self.hand. other hand pass function quietly ignored , self.hand used instead.
also this:
def blackjacksearch(self): if game.gettot(self.player.hand)==21: # ... this method belongs game type; , it’s instance method (taking self parameter). call static method game type not instance. should self.gettot() instead (you can leave out parameter per above).
you same @ other places too, trying call instance methods using typename.method. need have objects call them on.
i think can make gettot method lot shorter too:
def gettot(self,hand): total=0 x in self.hand: if x.number == 'a': if total + 11 > 21: total += 1 else: total += 11 elif x.number == 'j' or x.number == 'q' or x.number == 'k': pass # these? else: total += x.number return(total) rewriting parts of code:
class card (object): def __init__ (self, suit, number): self.suit = suit self.number = number def getvalue (self): if self.number in ('j', 'q', 'k'): return 10 elif self.number == 'a': return 11 else return int(self.number) def isace (self): return self.number == 'a' def __eq__ (self, other): return self.suit == other.suit , self.number == other.number def __str__ (self): return '%s %s' % (self.number,self.suit) class deckofcards (object): def __init__ (self, deck): self.fulldeck = deck self.shuffle() def shuffle (self): self.deck = self.fulldeck[:] # copy full deck random.shuffle(self.deck) def deal (self): if not len(self.deck): # need refill deck self.shuffle() return self.deck.pop(0) ls = [] suit in ('h', 's', 'c', 'd'): number in ('a', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k'): ls.append(card(suit, number)) your gettot method should belong player, , this:
def gettot(self): total = 0 card in self.hand: if card.isace() , total > 10: total += 1 else: total += card.getvalue() return total
Comments
Post a Comment