Syntax error with KeyError in python 3.2

I am a beginner using python 3.2, and I have book code that is all in python 2.6. I wrote part of the program and keep getting: Syntax error: invalid syntax Then IDLE python selects a comma after KeyError in my code:

from tank import Tank tanks = { "a":Tank("Alice"), "b":Tank("Bob"), "c":Tank("Carol")} alive_tanks = len(tanks) while alive_tanks > 1: print for tank_name in sorted( tanks.keys() ): print (tank_name, tanks[tank_name]) first = raw_input("Who fires? ").lower() second = raw_input("Who at? ").lower() try: first_tank = tanks[first] second_tank = tanks[second] except KeyError, name: print ("No such tank exists!", name) continue 
+7
source share
1 answer

Instead

 except KeyError, name: 

to try

 except KeyError as name: 

The difference between Python 2.x and Python 3.x. The first form is no longer supported.

+13
source

All Articles