[Tutor] robots question
Evert Rol
evert.rol at gmail.com
Thu Sep 16 17:57:48 CEST 2010
> As a exercise from this book ( Thinking like a computer scientist ) I have to make this programm on this page(http://openbookproject.net/thinkcs/python/english2e/ch12.html)
> Exercise 11
>
> #
> # robots.py
> #
> from gasp import *
Argh!
Is that really in the book?
Bad book, bad.
You can just "import gasp" and type "gasp.<function>" a few times.
<snip>Lot of code</snip>
> def play_game(robots):
> begin_graphics(SCREEN_WIDTH, SCREEN_HEIGHT)
> player = place_player()
> robot = place_robots(4)
> junk = [place_robot(GRID_WIDTH/2, GRID_HEIGHT/2, junk="true" )]
> defeated = False
>
> while not defeated:
> quit = move_player(player)
> if quit:
> break
> move_robots(robot, player)
> defeated = check_collisions(robot, player, junk)
>
> if defeated:
> remove_from_screen(player['shape'])
> for thing in robots + junk:
> remove_from_screen(thing['shape'])
> Text("They got you!", (240, 240), size=32)
> sleep(3)
>
> end_graphics()
>
>
>
> if __name__ == '__main__':
> play_game(2)
>
> But now Im getting this error message :
>
> Traceback (most recent call last):
> File "/root/workspace/test2/src/test.py", line 120, in <module>
> play_game(2)
> File "/root/workspace/test2/src/test.py", line 106, in play_game
> defeated = check_collisions(robot, player, junk)
> File "/root/workspace/test2/src/test.py", line 73, in check_collisions
> for thing in robots + junk:
> TypeError: can only concatenate list (not "dict") to list
>
> I understand that robots is a dict and junk is a list.
>
> Is that right ?
I think robots is not even a dict, but I may be wrong here.
Check your variable *names* inside play_game, and your use of '2' and '4'. Aren't you mixing up 'robot' and 'robots'?
Have you tried putting print statements inside the code? Also 'print type(<varname>)' can be variable.
Try more debugging when you get an exception.
> And why does the book say that when this message is appearing.
A quick glance at those webpages doesn't show any place where the book gives this code. This seems to be code you added.
Evert
More information about the Tutor
mailing list