if condition: # code for this case elif condition: # code for this case else: # code if no case is TrueIn your case you have the colon at the wrong position.
A
condition
could be everything which can return a bool
A condition for your if/elif statement can be
GPIO.input(7)
.You can do also the equality check
GPIO.input(7) == True
which returns also a boolean
.If I guess,
GPIO.input(7)
should return an integer object. An integer object with the value 0 it False. If it has the value 1, it's True. 1 == 1.0 == True
and 0 == 0.0 == False
.Your fixed code:
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(7,GPIO.IN) if GPIO.input(7)==True: print ("Secure") elif GPIO.input(7)==False: print ("Intruder alert")Or easier:
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(7,GPIO.IN) if GPIO.input(7): print ("Secure") else: print ("Intruder alert")We know that GPIO.input only returns 1 or 0.
There is no third case.
Little hint: If you want to check if an object is a boolean, just check it with
bool(your_object)
. If the object has the special method __bool__
, it returns True
or False
. Then you can use this object in an if/elif statement.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!