Python Forum

Full Version: Program not entering if statement on false.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
print('{0}'.format(authorindex) in votelist)
if '{0}'.format(authorindex) in votelist == False:
Now I've used the print function here to test the return value, and the return value is false. Which is correct as the purpose of this is to check if an index is in the dict, if not it will add it. However for some reason even with (if '{0}'.format(authorindex) in votelist == False:) it does not enter the if statement.

I should also note this is nested inside the else part of another if statement.

Never mind solved it, needed to use brackets to contain that segment of code.
You should not compare to True or False
The above code is better as
if not '{0}'.format(authorindex) in votelist:
(Nov-11-2018, 07:16 PM)buran Wrote: [ -> ]You should not compare to True or False
The above code is better as
if not '{0}'.format(authorindex) in votelist:

Why? As far as I can tell not means if the value is zero, same thing occurs with my code except if it can't find it, it returns false and I compare whatever result to false. Sure it looks neater and might be slightly quicker but I come from a background of C# and C++. So I prefer to not use not.

After some quick research, it seems people don't like using if (x == T/F) I honestly am baffled by this as I've used that all my coding time in C++ and C#. And I honestly think it is far easier to read and understand compared to not. But that's just my opinion.
(Nov-12-2018, 07:25 AM)Scottx125 Wrote: [ -> ]As far as I can tell not means if the value is zero
I am not sure what you meany by this. not will toggle the boolean, i.e. True will become False, and False will become True. I don't know what your reasoning not to use not in C/C++ is, but this is python.

'{0}'.format(authorindex) in votelist is True or False, i.e. already boolean.
using not '{0}'.format(authorindex) in votelist is more readable, cleaner, simple, etc. And does not require using brackets (i.e. your initial problem was due to interpreter evaluating from right to left, so your original code was actually equivalent to
if '{0}'.format(authorindex) in (votelist == False): - brackets added by me for explanation)

Also PEP8 is clear with recommendation in this case
Quote:Don't compare boolean values to True or False using ==.

Yes: if greeting:
No: if greeting == True:
Worse: if greeting is True:

and something else, I should have mentioned already
assuming authorindex is a variable that is not str and you want to check if it is in list that hold several str variables, it's better to use str() function, instead of string formatting (i.e. you use string formatting just to cast the autorindex to str).
if not str(authorindex) in votelist:
Quote:and something else, I should have mentioned already
assuming authorindex is a variable that is not str and you want to check if it is in list that hold several str variables, it's better to use str() function, instead of string formatting (i.e. you use string formatting just to cast the autorindex to str).
if not str(authorindex) in votelist:
Thanks for this. And in regards to the C++/C#. I find brackets look FAR neater and more readable than open lined code. I've only recently started programming in python from C++/C# and IMO python is not structured nicely. But that's my opinion. And the 'not' issue is personal preference as well, '== != >=' is more clear to me than not or is.