我做了一个树莓皮莫尔斯电码程序:
如何简化if语句:我想让程序播放用户键入的任何内容,但我不知道如何使用多个if语句。救命啊!!
我希望该程序能够发挥任何字的用户键入,但我不知道如何做到这一点。如果我需要给每个letgter分配一个变量,我该怎么做?你知道吗
import RPi.GPIO as GPIO
from time import sleep
LED_GPIO = 4
print("Getting ready...")
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_GPIO, GPIO.OUT)
def lighton(timeon):
print ("Light On - " , timeon)
GPIO.output(LED_GPIO, True)
sleep(timeon)
GPIO.output(LED_GPIO, False)
sleep(timeoff)
dashtime = .5
dottime = .25
timeoff = .1
x = 1
while x > 0:
let = input("Enter a letter or * to quit")
if let == "*":
x=0
elif let == "s":
lighton(dottime)
lighton(dottime)
lighton(dottime)
elif let == "o":
lighton(dashtime)
lighton(dashtime)
lighton(dashtime)
elif let == "sos":
lighton(s)
lighton(dashtime)
lighton(dashtime)
lighton(dashtime)
lighton(dottime)
lighton(dottime)
lighton(dottime)
else: print ("Letter not recognized - try again")
GPIO.cleanup()
print("Bye Bye")
使用字典来查找每个字符的flash模式,而不是
if
。你可以通过两个步骤来完成,一个是将一个字母转换成莫尔斯电码的dict,另一个是知道如何将其转换成闪光长度的dict。你知道吗相关问题 更多 >
编程相关推荐