Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax error with KeyError in python 3.2

I'm a beginner using python 3.2 and i have a book whos code is all in python 2.6. i wrote part of a program and keep getting: Syntax Error: invalid syntax Then python's IDLE highlights the 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
like image 647
ahmedalishaikh Avatar asked Sep 22 '11 19:09

ahmedalishaikh


1 Answers

Instead of

except KeyError, name:

try

except KeyError as name:

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

like image 163
Winston Ewert Avatar answered Oct 23 '22 10:10

Winston Ewert