Jag*_*son 0 python dictionary raspberry-pi
我实际上是为我的Raspberry Pi的GPIO端口编写一个控制程序,我在python中编程并想用字典来做.
好的,要设置一个GPIO端口,它需要2个参数,正如您在字典表中看到的那样,我使用了两个参数,当您编写类似"00"的内容时,您应该得到这两个参数.
例如,能够控制GPIO端口的代码是:
output(11, low)
Run Code Online (Sandbox Code Playgroud)
正如你在字典中看到的那样,我应该像上面的代码一样得到两个参数,但是我仍然得到一个错误,只有一个参数而不是两个.这是我的代码
import RPi.GPIO as GPIO
from RPi.GPIO import input as input
from RPi.GPIO import output as output
from RPi.GPIO import HIGH as high
from RPi.GPIO import LOW as low
from time import sleep as sleep
GPIO.setmode(GPIO.BOARD)
entry = raw_input("Which port you want to control?:")
while entry != "xx" :
io = {
'00' : "11, low",
'01' : "11, high",
'10' : "12, low",
'11' : "12, high",
'20' : "13, low",
'21' : "13, high",
'30' : "15, low",
'31' : "15, high",
'40' : "16, low",
'41' : "16, high",
'50' : "18, low",
'51' : "18, high",
'60' : "22, low",
'61' : "22, high",
'70' : "7, low",
'71' : "7, high",
'80' : "3, low",
'81' : "3, high",
'90' : "5, low",
'91' : "5, high",
'100' : "24, low",
'101' : "24, high",
'110' : "26, low",
'111' : "26, high",
'120' : "19, low",
'121' : "19, high",
'130' : "21, low",
'131' : "21, high",
'140' : "23, low",
'141' : "23, high",
'150' : "8, low",
'151' : "8, high",
'160' : "10, low",
'161' : "10, high"
}
output(io[entry])
entry = raw_input("Which port you want to control?:")
Run Code Online (Sandbox Code Playgroud)
代替:
io = {
'00' : "11, low",
'01' : "11, high",
# snip...
}
Run Code Online (Sandbox Code Playgroud)
制作你的价值元组:
io = {
'00': (11, low),
'01': (11, high)
# etc...
}
Run Code Online (Sandbox Code Playgroud)
然后将它们解压缩为您的参数output,例如:
output(*io[entry])
Run Code Online (Sandbox Code Playgroud)
目前,你试图传递output一个字符串,11, high而它似乎需要两个参数:一个整数和一个high或者low值之一.
边注:
我还将io循环中的赋值移动(没有点继续设置它)并使用双参数将其更改while为a ,例如:foriter
io = { ... }
for entry in iter(lambda: raw_input('Which port?: '), 'xx'):
output(*io[entry])
# rest of stuff...
Run Code Online (Sandbox Code Playgroud)