我已經用python編寫了一個程序,它接受字母,數字和字符,然後將它們轉換成莫爾斯電碼。該程序是爲我的覆盆子pi而編寫的,並使LED閃爍。麻煩在於用戶在輸入每個字符後必須按下輸入。我想這樣做,以便用戶可以鍵入完整的單詞和句子,然後用莫爾斯電碼閃現出來,但我不知道如何打破這些單詞。如何分解python中的單詞?
這裏是下面的代碼,我希望你能理解它,並幫助我:
print'Welcome to the Morse translator'
print'Enter the characters one by one pressing enter each time'
print'Enter the GPIO pin number that you would like to use'
pin = int (input())
while True:
name = raw_input("Enter a letter, number or character to see it in morse: ")
print name
import RPi.GPIO as gpio
import time
gpio.setmode(gpio.BCM)
gpio.setup(pin, gpio.OUT)
def dot():
gpio.output(pin, gpio.HIGH)
time.sleep(0.25)
gpio.output(pin, gpio.LOW)
time.sleep(0.25)
def dash():
gpio.output(pin, gpio.HIGH)
time.sleep(0.75)
gpio.output(pin, gpio.LOW)
time.sleep(0.25)
def e():
time.sleep(0.75)
if (name=='a'):
dot()
dash()
e()
elif (name=='b'):
dash()
dot()
dot()
dot()
e()
elif (name=='c'):
dash()
dot()
dash()
dot()
e()
elif (name=='d'):
dash()
dot()
dot()
e()
elif (name=='e'):
dot()
e()
elif (name=='f'):
dot()
dot()
dash()
dot()
e()
elif (name=='g'):
dash()
dash()
dot()
e()
elif (name=='h'):
dot()
dot()
dot()
dot()
e()
elif (name=='i'):
dot()
dot()
e()
elif (name=='j'):
dot()
dash()
dash()
dash()
e()
elif (name=='k'):
dash()
dot()
dash()
e()
elif (name=='l'):
dot()
dash()
dot()
dot()
e()
elif (name=='m'):
dash()
dash()
e()
elif (name=='n'):
dash()
dot()
e()
elif (name=='o'):
dash()
dash()
dash()
e()
elif (name=='p'):
dot()
dash()
dash()
dot()
e()
elif (name=='q'):
dash()
dash()
dot()
dash()
e()
elif (name=='r'):
dot()
dash()
dot()
e()
elif (name=='s'):
dot()
dot()
dot()
e()
elif (name=='t'):
dash()
e()
elif (name=='u'):
dot()
dot()
dash()
e()
elif (name=='v'):
dot()
dot()
dash()
e()
elif (name=='w'):
dot()
dash()
dash()
e()
elif (name=='x'):
dash()
dot()
dot()
dash()
e()
elif (name=='y'):
dash()
dot()
dash()
dash()
e()
elif (name=='z'):
dash()
dash()
dot()
dot()
e()
elif (name=='1'):
dot()
dash()
dash()
dash()
dash()
e()
elif (name=='2'):
dot()
dot()
dash()
dash()
dash()
e()
elif (name=='3'):
dot()
dot()
dot()
dash()
dash()
e()
elif (name=='4'):
dot()
dot()
dot()
dot()
dash()
e()
elif (name=='5'):
dot()
dot()
dot()
dot()
dot()
e()
elif (name=='6'):
dash()
dot()
dot()
dot()
dot()
e()
elif (name=='7'):
dash()
dash()
dot()
dot()
dot()
e()
elif (name=='8'):
dash()
dash()
dash()
dot()
dot()
e()
elif (name=='9'):
dash()
dash()
dash()
dash()
dot()
e()
elif (name=='0'):
dash()
dash()
dash()
dash()
dash()
e()
elif (name=='.'):
dot()
dash()
dot()
dash()
dot()
dash()
e()
elif (name==','):
dash()
dash()
dot()
dot()
dash()
dash()
e()
elif (name==':'):
dash()
dash()
dash()
dot()
dot()
dot()
e()
elif (name=='?'):
dot()
dot()
dash()
dash()
dot()
dot()
e()
elif (name=='-'):
dash()
dot()
dot()
dot()
dot()
dash()
e()
elif (name=='/'):
dash()
dot()
dot()
dash()
dot()
e()
elif (name=='(' or ')'):
dash()
dot()
dash()
dash()
dot()
dash()
e()
elif (name=='@'):
dot()
dash()
dash()
dot()
dash()
dot()
e()
elif (name=='='):
dash()
dot()
dot()
dot()
dash()
e()
elif (name==' '):
e()
e()
e()
e()
e()
e()
e()
else:
print'This character is not supported'
gpio.cleanup()
可能是bette r在這裏使用比許多'elif'字典更好的字典,例如''d = {'a':[dot,dash],'b':[dash,dot,dot,dot],...}'那麼你可以在d [letter]中調用'for f:f()' – jonrsharpe