Python Forum
What condition caused this True?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What condition caused this True?
#11
(Aug-21-2019, 02:17 AM)boring_accountant Wrote: You seem to be confusing English and Python, my friend. The basic idea in Python is that anything that follows if/elif/else are statements that are evaluated to True or False (I'm sure this isn't 100% true all the time but this should suffice for our example here). This means that "if True" does not mean literally "if [blank] is True then...", instead it means "if True is True then...".

Here:
#if True is True (True == True, so this is executed)
if True:
  print("1")
#else if True is True (True == True, but the previous statement always executes so this does not run)
elif True:
  print ("2")
#all other cases (will never happen, same reason)
else:
  print("3")
Now, the second example:
#If not True (not True == False) is True (False != True, so this does not run)
if not True:
  print("1")
#If not False (not False == True) is True (True == True, so this runs)
elif not False:
  print ("2")
#all other cases, this never runs, again
else:
  print("3")

Thank you for your reply. Now this makes sense to me, True or False is simply the condition of whether a command will be executed.
Reply
#12
Just to add one little thing, because it bugs me when I see it.

There is no need to directly compare things to "True". For example, none of these are "proper" although they will compile and are technically valid.

if thisThing == True:
All you need is:
if thisThing:
if thisThing not True:
All you need is:

if not thisThing:
On this one:
while True:
There is controversy. This creates what is called a "unbounded loop". It presents a possibility it will never exit. You could have this:

while True:
    if someThing:
        break;
A lot of coding practices and static analyzers will flag this as bad practice as if whatever is providing "someThing" goes wrong the while creates an infinite, run forever, loop.

In this case it's easy to fix, eg:

while not someThing:
If you want a "nearly" infinite loop and avoid "best practice" warnings, you can use the "while running" pattern.

running = True

while running:
    try:
        stuff
        otherstuff
    except Exception as e:
        log_exception()
        running = False
Hope this helps.
Reply
#13
(Sep-03-2019, 10:45 AM)venquessa Wrote:
while True:
    if someThing:
        break;

And if you don´t use ; in python code anymore you become a True pythonista. Big Grin
Reply
#14
As always I suggest before and above all to get acquainted with documentation: Truth Value Testing or typing help('TRUTHVALUE') into interactive interpreter. Maybe also help('for') and help('while') if you are into it.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#15
(Sep-03-2019, 11:24 AM)ThomasL Wrote:
(Sep-03-2019, 10:45 AM)venquessa Wrote:
while True:
    if someThing:
        break;

And if you don´t use ; in python code anymore you become a True pythonista. Big Grin

Damn, caught red handed :)

I'm a Java programmer by day. My own python code is also littered with someVariableName and PyCharm keeps whining at me :)

I'm only human not a python-esk god. :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UnicodeEncodeError caused by print when program runs from Popen SheeppOSU 5 2,911 Jan-13-2022, 08:11 AM
Last Post: SheeppOSU
  else condition not called when if condition is false Sandz1286 10 5,856 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,913 Jun-01-2020, 06:00 PM
Last Post: penahuse
  Returning True or False vs. True or None trevorkavanaugh 6 9,234 Apr-04-2019, 08:42 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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