Question:
I have this program that I wrote, but received some help shrinking from MNA4. I would like to know how the print statement in line 5 works. Now matter how hard I try, I can neither replicate it nor understand it, yet it works flawlessly in this program.
i,I,P=int,input,print
while 1:
try:
n,g,x=__import__('random').randint(i(I("Low num: ")),i(I("High num: "))),i,1
while g!=n: g=i(I("Your guess: "));P([[f"You found it in {x} tries!","Lower!"][g>n],"Higher!"][g<n]);x+=1
if I("Again? ").lower()in("n","no"):break
P('\033c')
except:P("\033cError")
I’m not sure my mind can grasp this. I got something similar working. I think I’ll just do the easy way and put if statements inside the print statement because this is too hard.
So, take [[a,b][d],c][e].
Python first evaluate what is inside the innermost list [a,b].
a first and b, second. So if one of those is a command, it gets executed.
Then is looks at [d] because it is an index. d can be a command or even a condition (remember in python true is 1, false is 0] . Based on [d] it will take a or b.
Then, python look at the second innermost list […, c] and does the same.
And looks at [e] and does the same.
The trick here is that you need to first look at the innermost list with index, then the one containing that, etc etc
To be honest this is pretty useless and using if then else is better