Ali_Waris
Ali_Waris

Reputation: 2382

Python - True part of if condition always executes

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.

Upvotes: 1

Views: 80

Answers (1)

Óscar López
Óscar López

Reputation: 236034

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.

Upvotes: 4

Related Questions