曾经遇到我的一个脚本问题。运行脚本时,在第23行下出现以下消息:
SyntaxError:缩进中的制表符和空格使用不一致
我尝试使用Anaconda将任何制表符替换为4/8空格,但没有成功。
有什么主意我要去哪里吗?
干杯。
import RPi.GPIO as GPIO
import time
sensor = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
previous_state = False
current_state = False
try:
while True:
time.sleep(0.1)
previous_state = current_state
current_state = GPIO.input(sensor)
if current_state != previous_state:
new_state = "HIGH" if current_state else "LOW"
print("GPIO pin %s is %s" % (sensor, new_state))
import smtplib
smtpUser = 'rpigroupproject@gmail.com'
smtpPass = 'W1r3l355'
toAdd = 'rpigroupproject@gmail.com'
fromAdd = smtpUser
subject = 'ALERT!'
header = 'To: ' + toAdd + '\n' + 'From: ' + \
fromAdd + '\n' + 'Subject:' + subject
body = 'Motion was detected...'
print header + '\n' + body
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(smtpUser, smtpPass)
s.sendmail(fromAdd, toAdd, header + '\n' + body)
s.quit()
except:
GPIO.cleanup()
答案 0 :(得分:0)
此处缩进固定。您应该配置编辑器,以免再次出现此错误。
import RPi.GPIO as GPIO
import time
sensor = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
previous_state = False
current_state = False
try:
while True:
time.sleep(0.1)
previous_state = current_state
current_state = GPIO.input(sensor)
if current_state != previous_state:
new_state = "HIGH" if current_state else "LOW"
print("GPIO pin %s is %s" % (sensor, new_state))
import smtplib
smtpUser = 'rpigroupproject@gmail.com'
smtpPass = 'W1r3l355'
toAdd = 'rpigroupproject@gmail.com'
fromAdd = smtpUser
subject = 'ALERT!'
header = 'To: ' + toAdd + '\n' + 'From: ' + fromAdd + '\n' + 'Subject:' + subject
body = 'Motion was detected...'
print header + '\n' + body
s = smtplib.SMTP('smtp.gmail.com',587)
s.ehlo()
s.starttls()
s.ehlo()
s.login(smtpUser, smtpPass)
s.sendmail(fromAdd, toAdd, header + '\n' + body)
s.quit()
except:
GPIO.cleanup()
答案 1 :(得分:0)
我不确定您是否使用自定义标识,但只是出于测试目的,我建议您重置所有自定义并使用 tabs 。
例如,这是您的以下代码示例。您会看到 try-while 和 while 之后的标识不一致
try:
while True:
time.sleep(0.1)
previous_state = current_state
current_state = GPIO.input(sensor)
具有一致标识的外观如下:
try:
while True:
time.sleep(0.1)
previous_state = current_state
current_state = GPIO.input(sensor)