2014-01-25 46 views
0

我已經用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() 
+2

可能是bette r在這裏使用比許多'elif'字典更好的字典,例如''d = {'a':[dot,dash],'b':[dash,dot,dot,dot],...}'那麼你可以在d [letter]中調用'for f:f()' – jonrsharpe

回答

0

把你的名字的變量,運行一個for循環的每個單詞的長度,貫穿每個字符的如果循環?

1

字符串基本上是字符列表,因此您可以遍歷它們來分隔每個字符。

>>> name = 'This is a test' 
>>> c = list() 
>>> for x in name: 
     c.append(x) 
>>> print c 
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't'] 

或者像列表理解:

>>> c = [x for x in name ] 
>>> print c 
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't'] 

此外,我會寫一個單獨的函數每個字符轉換爲莫爾斯信號:

>>> def char_to_morse(char): 
     signals = {'a':[dash, dot] ...} 
     for f in signals: f() 

,最後只是把它叫做:

>>> for x in name: char_to_morse(x)