Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statement falsely triggered

I have a python script parse an incoming payload at set a variable named "confidence" and "id" then it will evaluate if the id is equal to sam@sam and that confidence is greater than or equal to 70. Now the id is equal sam@sam but confidence is not, it will still trigger! whats wrong with my code!!?

p = eg.event.payload[2]
p = p.split(',')
id = p[0].strip()
confidence = p[1].strip()
print confidence
url = p[2].strip()
if confidence >= 70 and id == "sam@sam":
  eg.TriggerEvent("FaceRec", "Unlock Door" )
else:
  eg.plugins.GoogleVoice.SendSMS(u'407#####', url)
like image 532
user2266621 Avatar asked Nov 24 '25 08:11

user2266621


2 Answers

The problem is that "confidence" is a string and you are comparing it to a number. The result of comparing string to a number is consistent, but not necessarily the one you want.

Fix:

confidence = int(p[1].strip())
like image 186
mishik Avatar answered Nov 25 '25 20:11

mishik


Ok, let's take a closer look at your code. Now, p is a string. So when you split it, the results(id and confidence) are strings. That's your problem. Since confidence is still a string when you compare the values, you are comparing a string and an int. Do this:

confidence = int(p[1].strip())

This converts the string to an int first, so, confidence will be an int. The comparison will then work properly.

like image 38
kirbyfan64sos Avatar answered Nov 25 '25 20:11

kirbyfan64sos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!