我是 python 新手,我已经为我的 Raspberry Pi 上的 GPIO 引脚编写了一些代码,但是当我运行它时,我得到了这个:
user@pi:~$ sudo python change.py
Which GPIO do you whant to change? 13
Exit (y/n) n
Traceback (most recent call last):
File "change.py", line 36, in <module>
runGPIO()
File "change.py", line 31, in runGPIO
var_input2 = input ("Exit (y/n) ")
File "<string>", line 1, in <module>
NameError: name 'n' is not defined
因为我是 python 新手,所以我似乎找不到任何错误——即使有错误检查器,所以我有点困惑代码是......
import sys
import RPi.GPIO as GPIO
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.output(13, True)
GPIO.output(15, True)
GPIO.output(16, True)
var13 = True
var15 = True
var16 = True
def runGPIO():
var_input = input ("Which GPIO do you whant to change? ")
if (var_input == 13) or (var_input == 2):
if var13 == True:
GPIO.output(13, False)
else:
GPIO.output(13, True)
elif (var_input == 15) or (var_input == 3):
if var15 == True:
GPIO.output(15, False)
else:
GPIO.output(15, True)
elif (var_input == 16) or (var_input == 4):
if var16 == True:
GPIO.output(16, False)
else:
GPIO.output(16, True)
else:
print "Please enter a valid GPIO pin number"
var_input2 = input ("Exit (y/n) ")
if var_input2 == "y":
sys.exit()a
else:
return
runGPIO()
代码集设置了 3 个 GPIO 引脚(将它们打开)。然后代码会在询问用户打开或关闭哪个 LED 之前关闭所有 LED
任何有关改进等的帮助建议将不胜感激
谢谢