I couldn't reproduce the exact error message you received, but I will note:
(1) your splitting of lines leaves out the chain, so chain is never defined
(2) line 9 of your python is missing an opening quotation mark before (resi matching the closing quotation mark right before the % sign
(3) line 9 has an error in that chain is the first argument in your tuple, but should be the last argument based on the string format you are substituting into.
(4) line 10 above cmd.distance("d","sele and n. CA, sele and n. CA")
you need to have the second and third arguments as part of one string. These should be two separate arguments in python:
cmd.distance("d","sele and n. CA", "sele and n. CA")
(5) cmd.color("sele", red)
(a) you switched the order of the arguments for some reason from what you showed in your pymol commands
(b) red needs to be in quotation marks
(6) cmd.save("%s_%s".pse)%(pdbid,chain) ; you need to have .pse inside the quotation marks, it is part of the string. Also, your parentheses matching places the string substitution tuple outside the arguments of the function.
(7) cmd.png("%s_%s")%(pdbid,chain) again, the string substitution tuple is outside the function
as an example, you want cmd.png( "%s_%s"%(pdbid,chain) )
so you produce your string inside the parentheses corresponding to the command.
(8) cmd.delete(pdbid.split(',')) ; you probably want to split on the period, not a comma.
The line numbers in the error messages don't match the python you have pasted. It seems the python you pasted is not what actually produced the error message given.
Your off to a good start. Generally, you should look at the last few lines of the error message, this will tell you what line number your error is on and give you a clue what is happening. You can then start reading backwards through the message to see if it gives more information. Since line numbers are so important, you should use something to write your script that has line numbers. There are many text editors that do so. One with syntax highlighting for python would also be useful as it will make many of the issues much more apparent.