Python Forum
Program not entering if statement on false.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program not entering if statement on false.
#1
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.
Reply
#2
You should not compare to True or False
The above code is better as
if not '{0}'.format(authorindex) in votelist:
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Reply
#4
(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:
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Eliminate entering QR - Whatsapp web automated by selenium akanowhere 1 3,086 Jan-21-2024, 01:12 PM
Last Post: owalahtole
  problem in entering address of a file in input akbarza 0 651 Oct-18-2023, 08:16 AM
Last Post: akbarza
  getting an import statement to work in my program barryjo 1 1,662 Dec-06-2021, 04:28 PM
Last Post: snippsat
  Why is the line crashing program when line is false? ErnestTBass 1 1,794 Aug-23-2020, 07:09 PM
Last Post: deanhystad
  syntaxerror when entering a constructor MaartenRo 2 1,986 Aug-03-2020, 02:09 PM
Last Post: MaartenRo
  difference between «1 in [2] == False» and «(1 in [2]) == False» fbaldit 2 2,237 Apr-20-2020, 05:39 PM
Last Post: fbaldit
  How to Stop Sentinel Value from Entering Final Output ZQ12 3 3,240 Nov-11-2019, 07:25 AM
Last Post: perfringo
  Error when entering letter/character instead of number/integer helplessnoobb 2 7,076 Jun-22-2019, 07:15 AM
Last Post: ThomasL
  creating a username and pword program using a #def statement and #dictionary zcode12 3 3,149 Oct-14-2018, 04:41 AM
Last Post: volcano63
  Entering an expression with "input" command johnmnz 3 3,495 Sep-01-2017, 05:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020