Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"unresolved reference" in ternary if else one liner

I am trying to work out why the following line doesn’t work:

i = 0 if i // 3 == 0 else i += 1

I am getting an "unresolved reference ‘i’ " error despite the fact that i is defined before this line. Apologies if this is obvious but I am pretty new to ternaries and haven’t been able to find a good reason why this doesn’t work (although I suspect it may be something to do with the augmented assignment).

Thanks in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should change this to i = 0 if i // 3 == 0 else i + 1. The reason for this is that it parses as i = (0 if i // 3 == 0 else i + 1), not (i = 0) if (i // 3 == 0) else (i + 1) as you seem to assume.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading