[Tutor] getting nasty TypeError:

Dave Angel d at davea.name
Sun Nov 6 03:46:13 CET 2011

On 11/05/2011 10:02 PM, Nathaniel Trujillo wrote:
> Hello. I am currently working with Python version 3.1.1 . I am working out
> of the book called Python Programming for the Absolute Beginner Third
> Edition which teaches version 3.1.1 . I wrote the following blackjack
> program that is on page 275 and I imported the modules that you see below.
> First the cards module which is on page 271 and then the games module which
> is on page 268. I tried typing everything in exactly the way it is in the
> book but when I run the blackjack program that you see below it will first
> ask me how many players just like it should, then I tell it how many
> players, then it will ask me the names of each player just like it should,
> then I tell it the name of each player and after I do that and press enter
> I get the following error message.
> Traceback (most recent call last):
>    File "C:\Python31\blackjack.py", line 184, in<module>
>      main()
>    File "C:\Python31\blackjack.py", line 181, in main
>      game.play()
>    File "C:\Python31\blackjack.py", line 132, in play
>      print(player)
>    File "C:\Python31\blackjack.py", line 34, in __str__
>      rep = self.name + "\t" + super(BJ_Hand, self).__str__()
> TypeError: can only concatenate list (not "str") to list
Did you omit the __init__() method on some of these classes?  For 
example, I don't see one for BJ_Player, and yet instances are created 
with a name argument.

Anyway, let me point you to a standard debugging technique which should 
be introduced by page 10 of any such book:

You have an error message which is complaining about a specific line, 
and the error message is complaining about the types of the objects in 
the expression on that line.,  So add some print()s, to show type and 
value (usually repr() is good enough) for the items in the statement.

Judging from the wording, self.name is a list, rather than a string as 
expected.  Given that, work backwards to see how it got its value.



More information about the Tutor mailing list