我正在写一个小莫尔斯代码翻译器。
但出于某种原因,它无法正常工作。
由于某种原因,广播部分无法正常工作...... 我无法得到它。欢迎任何帮助!
以下是代码:
#import RPi.GPIO as GPIO
import re,time, pdb
def get_user_text():
user_text = raw_input("Please enter the message you would lile to broadcast. >> ")
user_text = user_text.lower()
word_list = list(user_text)
return word_list
def text_to_morse_code(alpha_text):
morse_code = []
for letter in alpha_text:
if letter == "a" or letter == "à" or letter == "â" or letter == "ä":
morse_code.append("01")
if letter == "b":
morse_code.append("1000")
if letter == "c":
morse_code.append("1010")
if letter == "d":
morse_code.append("100")
if letter == "e" or letter == "è" or letter == "é" or letter == "ê":
morse_code.append("0")
if letter == "f":
morse_code.append("0010")
if letter == "g":
morse_code.append("110")
if letter == "h":
morse_code.append("0000")
if letter == "i" or letter == "î" or letter == "ï":
morse_code.append("00")
if letter == "j":
morse_code.append("0111")
if letter == "k":
morse_code.append("101")
if letter == "l":
morse_code.append("0100")
if letter == "m":
morse_code.append("11")
if letter == "n":
morse_code.append("10")
if letter == "o":
morse_code.append("111")
if letter == "p":
morse_code.append("0110")
if letter == "q":
morse_code.append("1101")
if letter == "r":
morse_code.append("010")
if letter == "s":
morse_code.append("111")
if letter == "t":
morse_code.append("0")
if letter == "u":
morse_code.append("001")
if letter == "v":
morse_code.append("0001")
if letter == "w":
morse_code.append("011")
if letter == "x":
morse_code.append("1001")
if letter == "y":
morse_code.append("1011")
if letter == "z":
morse_code.append("1100")
if letter == ".":
morse_code.append("010101")
if letter == " ":
morse_code.append(" ")
else:
pass
morse_code = ''.join(map(str, morse_code))
morse_code = list(morse_code)
return morse_code
def broadcast_code(code_to_broadcast, pin):
# Set the board as BOARD
#GPIO.setmode(GPIO.BOARD)
print("Set the board to BOARD")
# Setup the n th pin to OUTPUT
#GPIO.setup(pin, GPIO.OUT)
print("Set the "+str(pin)+"th to OUTPUT")
# Starting the broadcast
print("Starting Broadcast")
start_broadcast = [0,1,0,1]
for number in start_broadcast:
if number == 1:
#GPIO.output(pin,True)
time.sleep(1)
#GPIO.output(pin, False)
print(number)
if number == 0:
#GPIO.output(pin,True)
time.sleep(0.5)
#GPIO.output(pin, False)
print(number)
print("Broadcasting")
code_to_broadcast = code_to_broadcast
for number in code_to_broadcast:
if number == 1:
#GPIO.output(pin,True)
time.sleep(1)
#GPIO.output(pin, False)
print(number)
if number == 0:
#GPIO.output(pin,True)
time.sleep(0.5)
#GPIO.output(pin, False)
print(number)
#Boardcast end of transmission.
print("Ending Boardcast")
end_broadcast = [0,0,0,1,0,1]
for number in end_broadcast:
if number == 1:
#GPIO.output(pin,True)
time.sleep(1)
#GPIO.output(pin, False)
print(number)
if number == 0:
#GPIO.output(pin,True)
time.sleep(0.5)
#GPIO.output(pin, False)
print(number)
#GPIO.cleanup()
print("Cleaned up the board.")
def get_code_broadcast():
#
#GPIO.output(pin,True)
print("Hello")
if __name__ == '__main__':
code = get_user_text()
code = text_to_morse_code(code)
broadcast_code(code,7)
我得到的输出:
$ Please enter the message you would lile to broadcast. >> Hello
Set the board to BOARD
Set the 7th to OUTPUT
Starting Broadcast
0
1
0
1
Broadcasting
Ending Boardcast
0
0
0
1
0
1
Cleaned up the board.
答案 0 :(得分:1)
dict示例:
morse_dict = {
'a': '01',
'b': '1000',
'c': '1010',
}
def get_morse_code(text):
morse_code = []
for n in text:
morse_code.append(morse_dict[n])
return morse_code
现在你可以做到
>>> print(get_morse_code('abcba'))
你需要做的就是用所有莫尔斯的东西来扩展字典。
答案 1 :(得分:0)
你的问题是关于这个循环:
for number in code_to_broadcast:
if number == 1:
#GPIO.output(pin,True)
time.sleep(1)
#GPIO.output(pin, False)
print(number)
if number == 0:
#GPIO.output(pin,True)
time.sleep(0.5)
#GPIO.output(pin, False)
print(number)
变量code_to_broadcase
是一个字符串。这是你问题的根源。
字符串"0"
不等于Python中的整数0
。同样,"1"
不等于1
。如果您修改了比较,您的代码将起作用:
for number in code_to_broadcast:
if number == "1": # test against a string
#GPIO.output(pin,True)
time.sleep(1)
#GPIO.output(pin, False)
print(number)
if number == "0": # here too
#GPIO.output(pin,True)
time.sleep(0.5)
#GPIO.output(pin, False)
print(number)
或者,您可以将字符串更改为整数列表,并使用未修改的当前循环代码。
请注意,因为您的两个区块之间的唯一区别是您睡觉的时间量,您可以通过简单地条件化延迟量来简化操作:
for number in code_to_broadcast:
#GPIO.output(pin,True)
time.sleep(1 if number == "1" else 0.5) # use a conditional expression
#GPIO.output(pin, False)
print(number)
还有许多其他地方可以改进您的代码(主要是通过分解重复编码)。我强烈建议使用字典来存储字母和莫尔斯代码dits和dahs之间的转换,而不是使用很长的一系列if
语句。 (如果确实保留了if
链,那么第一个if
之后的所有elif
都应该是elif
s,因为只有先前的True
没有,它们才会成立。使用{{1}}将允许Python停止测试后面的条件,如果之前的条件是{{1}}。)