I am using python2.7, and I was wondering what the argument is for interpolating pythons strings with tuples. When I was doing this little bit of code, I was getting TypeErrors :
def show(self): self.score() print "Player has %s and total %d." % (self.player,self.player_total) print "Dealer has %s showing." % self.dealer[:2]
Print
Player has ('diamond', 'ten', 'diamond', 'eight') and total 18 Traceback (most recent call last): File "trial.py", line 43, in <module> Blackjack().player_options() File "trial.py", line 30, in player_options self.show() File "trial.py", line 27, in show print "Dealer has %s showing." % (self.dealer[:2]) TypeError: not all arguments converted during string formatting
So, I found that I need to change the fourth line where the error came from:
print "Dealer has %s %s showing." % self.dealer[:2]
With two %s statements, one for each item in the collection piece. When I checked what happens with this line, although I added to print type(self.dealer[:2]) and would get:
<type 'tuple'>
As I expected, why not a sliced ββtuple like the Player has %s and total %d." % (self.player,self.player_total) format Player has %s and total %d." % (self.player,self.player_total) , and a sliced ββtuple self.dealer[:2] not? They are both of the same type , so don't skip a slice without explicitly formatting each element in the slice?
source share