Python - True part of if condition always executes

Ali_Waris

No matter what's the value of what, the true part of if condition is always executed.

what = ""
def sendPacket(where, what):
        print("sendPacket()> i/p what :" + what)
        if what:
            zb.send('tx',dest_addr_long = where,dest_addr = UNKNOWN,data = what)
            print('sendPacket()> Data: '+ what) 
            print('sendPacket()> Data sent to: '+ where )
        else:
            print('sendPacket()> data not sent')

I want the true part to be executed only when what is not equal to null or empty. Any help would be appreciated.

Óscar López

Ask explicitly for the condition you want:

if what and not what.isspace():

The above states that what is not null, is not an empty string, and is not a string of just whitespaces. It's possible that your input has some line break, tab, or other non-printable character that's messing with the test.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

if condition always evaluating true

From Dev

Term for an if condition that is always true

From Dev

if condition always returns true (Java)

From Dev

Why is this PHP if condition always true?

From Dev

Why is this PHP if condition always true?

From Dev

awk condition always TRUE in a loop

From Dev

Bash Script if condition is always true

From Dev

When if/else condition is true, executes code in true and false blocks

From Dev

Else block of javascript program always executes even If block is true

From Dev

Condition always true when reached in while loop

From Dev

SQL Server - ignoring always true condition

From Dev

Why does my condition always evaluate as true?

From Dev

Compiler: What if condition is always true / false

From Dev

C++ GUI Condition for if statement always true

From Dev

WIX Operating System Condition Always true

From Dev

SQL Server - ignoring always true condition

From Dev

Why a condition that contains a pointer always returns true?

From Dev

Why is my "if" condition (string comparison) always true?

From Dev

Is `0 is 0` always `True` in Python?

From Dev

ajax if condition not working properly,always working else part

From Dev

Intellij Idea hint: Condition is always false - can that be true here? (Java)

From Java

Boolean condition is always false when using `status == true`

From Java

Typescript error This condition will always return 'true' since the types have no overlap

From Dev

if condition in try block always turns out true even if it shouldn´t

From Dev

SonarLint : Change this condition so that it does not always evaluate to "true"

From Dev

if condition in try block always turns out true even if it shouldn´t

From Dev

When I upload an Image in PHP there's a condition that always true

From Dev

php mail function is not working even its showing true condition part

From Dev

php mail function is not working even its showing true condition part

Related Related

  1. 1

    if condition always evaluating true

  2. 2

    Term for an if condition that is always true

  3. 3

    if condition always returns true (Java)

  4. 4

    Why is this PHP if condition always true?

  5. 5

    Why is this PHP if condition always true?

  6. 6

    awk condition always TRUE in a loop

  7. 7

    Bash Script if condition is always true

  8. 8

    When if/else condition is true, executes code in true and false blocks

  9. 9

    Else block of javascript program always executes even If block is true

  10. 10

    Condition always true when reached in while loop

  11. 11

    SQL Server - ignoring always true condition

  12. 12

    Why does my condition always evaluate as true?

  13. 13

    Compiler: What if condition is always true / false

  14. 14

    C++ GUI Condition for if statement always true

  15. 15

    WIX Operating System Condition Always true

  16. 16

    SQL Server - ignoring always true condition

  17. 17

    Why a condition that contains a pointer always returns true?

  18. 18

    Why is my "if" condition (string comparison) always true?

  19. 19

    Is `0 is 0` always `True` in Python?

  20. 20

    ajax if condition not working properly,always working else part

  21. 21

    Intellij Idea hint: Condition is always false - can that be true here? (Java)

  22. 22

    Boolean condition is always false when using `status == true`

  23. 23

    Typescript error This condition will always return 'true' since the types have no overlap

  24. 24

    if condition in try block always turns out true even if it shouldn´t

  25. 25

    SonarLint : Change this condition so that it does not always evaluate to "true"

  26. 26

    if condition in try block always turns out true even if it shouldn´t

  27. 27

    When I upload an Image in PHP there's a condition that always true

  28. 28

    php mail function is not working even its showing true condition part

  29. 29

    php mail function is not working even its showing true condition part

HotTag

Archive