Python [false,true][if condition] functionning

Hello !

They were a clash where you have to invert
the vowel “aeiouy” (a->y, e->u,…y->a)
the consonant “bcdfghjklmnpqrstvwxz” (b->z, c->x,…z->b)

Example: “Yllpu” → “Apple”

I try to reduce the code with the [false,true][if condition] in python but for some reason it doesn’t work and i don’t understand why. I try different solution that works (S1 and S2) but not S3 which is the same if condition. (or maybe i made a mistake that i didn’t see)

#---------------Input--------------------
z="aeiouy bcdfghjklmnpqrstvwxz"
y=z.split()
s="Yllpu"

#---------------Solution1: works--------------------
a=""
for x in s.lower():
    if x in y[0]:
        a+=y[0][5-y[0].find(x)]
    else:
        a+=y[1][19-y[1].find(x)]
print(a.capitalize())

#---------------Solution2: works--------------------
print("".join(y[0][5-y[0].find(x)]if x in y[0]else y[1][19-y[1].find(x)] for x in s.lower()).capitalize())

#---------------Solution3: don't works--------------------
print("".join([y[1][19-y[1].find(x)],y[0][5-y[0].find(x)]][x in y[0]]for x in s.lower()).capitalize())

See if this code is similar to your situation?

y = [4, 5, 6]
x = 3
print((1/0) if x in y else 1)
print([1, 1/0][x in y])

Try this:

v="aeiouya"
print(v[v.index("a")+1])

Hello, what happens is the following certainly you have a conditional for each case, in case of vowels or consonants, but the array at the time of its declaration will create the position a or b independent of the condition, ie the position in memory that involves the position [a, b] which exists therefore it is executing the search of your index, then if you have a case where for example it is a consonant and therefore when doing find in the vowels it will send you a -1 and likewise you have within that condition 5-y[1]. find(x), if we translate that you would have 5–1 if in python you put a negative in front of it it will multiply and you will have 5+1.

That because the conditional does not omit that both positions in memory are going to be created and in this case you would be looking for the 6th position where it does not exist.

You have several ways to solve it, the first one would be to add a character that will never appear in the string both in volcales and consonants, for example :
z=“aeiouy@ bcdfghjklmnpqrstvwxz@”

another option, is that you put a max at the time of the subtraction and thus omit the -1 “max(y[0].find(x),0)”.

lastly simply use the common conditional as in the second case

Translated with www.DeepL.com/Translator (free version)

#---------------Input--------------------
z="aeiouy@ bcdfghjklmnpqrstvwxz@"
y=z.split()
s="Yllpu"

#---------------Solution1: works--------------------
a=""
for x in s.lower():
    if x in y[0]:
        a+=y[0][5-y[0].find(x)]
    else:
        a+=y[1][19-y[1].find(x)]
print(a.capitalize())

#---------------Solution2: works--------------------
print("".join(y[0][5-y[0].find(x)]if x in y[0]else y[1][19-y[1].find(x)] for x in s.lower()).capitalize())

#---------------Solution3: don't works--------------------
print("".join([y[1][19-max(y[1].find(x),0)],y[0][5-max(y[0].find(x),0)]][x in y[0]]for x in s.lower()).capitalize())
1 Like

Thank you for your answer and thank you a lot AlexWord for the explanation !
That was a bit tricky but i understand now :slight_smile: