[Tutor] If/else in Python 2.6.5 vs. Python 2.4.3

Dave Angel davea at ieee.org
Mon Oct 11 23:26:55 CEST 2010


On 2:59 PM, aeneas24 at priest.com wrote:
> 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:
>            []
>
>
close.  The else clause should read:
                          rgenre = []
> 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
>


More information about the Tutor mailing list