簡體   English   中英

python中的GPIO條件

[英]GPIO conditional in python

我在python中編寫子條件時遇到了一些麻煩。 希望有人可以查看我的代碼:

第一個條件是讀取GPIO(24),另一個條件是文件存在。 如果文件存在,則無需執行任何操作。

這是我的初始代碼:#!/ usr / bin / python

import os
import RPi.GPIO as GPIO
from time import sleep     
GPIO.setmode(GPIO.BCM)     
GPIO.setup(24, GPIO.IN)    
GPIO.setup(22, GPIO.OUT)   
file = "22.gp"


try:
        while True:            
                if GPIO.input(24): 
                print "Port 24 is 1/HIGH/True - LED ON"
                    if os.path.exists(file)
                    print "Already light ON"
                    else:
                    GPIO.output(22, 1)
                    file = open(file, "w+")
            sleep(20)
    else:
            print "Port 24 is 0/LOW/False - LED OFF"
            GPIO.output(22, 0)         
                    if os.path.exists(file):
                    os.remove(file)
                    else:
                    print("sorry, I cannot remove %s file:" % file)
            sleep(0.1)         

finally:                   
    GPIO.cleanup()   

這段代碼給出了這樣的錯誤:

print "Port 24 is 1/HIGH/True - LED ON"  
        ^
IndentationError: expected an indented block

由於python不使用花括號或分號,因此需要了解語句縮進的范圍和結尾。

if GPIO.input(24): 
print "Port 24 is 1/HIGH/True - LED ON"

更改為那個(所有的if / else,循環):

if GPIO.input(24): 
    print "Port 24 is 1/HIGH/True - LED ON"

標准使用4個空間。 跟着它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM