我在python中编写了一个程序,它接收字母,数字和字符,然后将它们转换为摩尔斯电码。该程序是为我的覆盆子pi编写的,并打开和关闭LED闪光灯。问题是用户在键入每个字符后必须按Enter键。我想这样做,以便用户可以输入整个单词和句子,然后用摩尔斯电码闪过,但我不知道如何打破这些话。
下面的代码如下,我希望你能理解它并帮助我:
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()
答案 0 :(得分: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)
答案 1 :(得分:0)
拿你的名字变量,运行每个单词长度的for循环,通过if循环运行每个字符?