[Tutor] getting nasty TypeError:

Steven D'Aprano steve at pearwood.info
Sun Nov 6 03:48:28 CET 2011


Nathaniel Trujillo wrote:

> 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
> 
> Here is the blackjack program. It is called blackjack.py

Oooh, that was a tricky one! And yet the bug turned out to be a tiny 
little thing...

In your blackjack.py module, in the main() function, change the line

     names.append(names)

to

     names.append(name)



For what it's worth, how I debugged this was to insert the line:

print(names)

in the main() function, and saw that it was printing [[...]] instead of 
a list of strings. Once I realised that the main function was screwing 
up the list of names, it was easy to solve.


-- 
Steven



More information about the Tutor mailing list