[Tutor] If/else in Python 2.6.5 vs. Python 2.4.3
aeneas24 at priest.com
aeneas24 at priest.com
Mon Oct 11 19:10:11 CEST 2010
Hi,
I have code that works fine when I run it on Python 2.6.5, but I get an "invalid syntax" error in Python 2.4.3. I'm hoping you can help me fix it.
The line in question splits a chunk of semi-colon separated words into separate elements.
rgenre = re.split(r';', rf.info["genre"] if "genre" in rf.info else []
I get a syntax error at "if" in 2.4.3.
I tried doing the following but it doesn't work (I don't understand why).
if "genre" in rf.info:
rgenre = re.split(r';', rf.info["genre"])
else:
[]
And I tried this, too:
if "genre" in rf.info:
rgenre = re.split(r';', rf.info["genre"])
if "genre" not in rf.info:
[]
Both of these cause problems later on in the program, specifically "UnboundLocalError: local variable 'rgenre' referenced before assignment", about this line in the code (where each of these is one of the 30 possible genres):
rg1, rg2, rg3, rg4, rg5, rg6, rg7, rg8, rg9, rg10, rg11, rg12, rg13, rg14, rg15, rg16, rg17, rg18, rg19, rg20, rg21, rg22, rg23, rg24, rg25, rg26, rg27, rg28, rg29, rg30= rgenre + ["NA"]*(30-len(rgenre[:30]))
Thanks for any help--I gave a little extra information, but I'm hoping there's a simple rewrite of the first line I gave that'll work in Python 2.4.3.
Thanks,
Tyler
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101011/1976c07f/attachment-0001.html>
More information about the Tutor
mailing list