为什么此代码在没有任何输入的情况下在启动时显示 'A'?
Why does this code display 'A' on start without any input?
这是 microbit 的摩尔斯电码翻译器,但它在启动时显示 'A'
from microbit import *
morse={'.-': 'A', '-...': 'B', '-.-.': 'C', '-..': 'D', '.': 'E', '..-.': 'F', '--.': 'G', '....': 'H', '..': 'I', '.---': 'J', '-.-': 'K', '.-..': 'L', '--': 'M', '-.': 'N', '---': 'O', '.--.': 'P', '--.-': 'Q', '.-.': 'R', '...': 'S', '-': 'T', '..-': 'U', '...-': 'V', '.--': 'W', '-..-': 'X', '-.--': 'Y', '--..': 'Z', '.----': '1', '..---': '2', '...--': '3', '....-': '4', '.....': '5', '-....': '6', '--...': '7', '---..': '8', '----.': '9', '-----': '0', '--..--': ', ', '.-.-.-': '.', '..--..': '?', '-..-.': '/', '-....-': '-', '-.--.': '(', '-.--.-': ')'}
message=''
while True:
morseChr=''
if button_a.is_pressed:
morseChr+='.'
if button_b.is_pressed:
morseChr+='-'
if button_a.is_pressed and button_b.is_pressed:
message+=morse[morseChr]
display.show(message)
sleep(1000*len(message))
display.clear()
我希望它能将按钮按下转换为一条消息,但它只显示 'A'
你现在的逻辑有两个问题:
首先,只要您同时按 A 和 B,.-
就会添加到您的消息中。为避免这种情况,请使用 else if
并首先移动 A 和 B 案例(因为这应该比 A 或 B 具有更高的优先级)。
其次,除了 A 之外,您实际上永远不能在消息中添加任何其他字符,因为您的 morseChar
在每个循环中都被重置为空字符串。您需要将变量移出循环以跟踪先前的输入。
此外,is_pressed 是 microbit 文档中的函数。
生成的代码如下所示:
message=''
morseChr=''
while True:
if button_a.is_pressed() and button_b.is_pressed():
# First check if the entered char is actually valid
if morseChr not in morse:
morseChr='' # reset chars to avoid being stuck here endlessly
# maybe also give feedback to the user that the morse code was invalid
continue
# add the char to the message
message += morse[morseChr]
morseChr=''
display.show(message)
sleep(1000*len(message))
display.clear()
elif button_a.is_pressed():
morseChr+='.'
elif button_b.is_pressed():
morseChr+='-'
这是 microbit 的摩尔斯电码翻译器,但它在启动时显示 'A'
from microbit import *
morse={'.-': 'A', '-...': 'B', '-.-.': 'C', '-..': 'D', '.': 'E', '..-.': 'F', '--.': 'G', '....': 'H', '..': 'I', '.---': 'J', '-.-': 'K', '.-..': 'L', '--': 'M', '-.': 'N', '---': 'O', '.--.': 'P', '--.-': 'Q', '.-.': 'R', '...': 'S', '-': 'T', '..-': 'U', '...-': 'V', '.--': 'W', '-..-': 'X', '-.--': 'Y', '--..': 'Z', '.----': '1', '..---': '2', '...--': '3', '....-': '4', '.....': '5', '-....': '6', '--...': '7', '---..': '8', '----.': '9', '-----': '0', '--..--': ', ', '.-.-.-': '.', '..--..': '?', '-..-.': '/', '-....-': '-', '-.--.': '(', '-.--.-': ')'}
message=''
while True:
morseChr=''
if button_a.is_pressed:
morseChr+='.'
if button_b.is_pressed:
morseChr+='-'
if button_a.is_pressed and button_b.is_pressed:
message+=morse[morseChr]
display.show(message)
sleep(1000*len(message))
display.clear()
我希望它能将按钮按下转换为一条消息,但它只显示 'A'
你现在的逻辑有两个问题:
首先,只要您同时按 A 和 B,.-
就会添加到您的消息中。为避免这种情况,请使用 else if
并首先移动 A 和 B 案例(因为这应该比 A 或 B 具有更高的优先级)。
其次,除了 A 之外,您实际上永远不能在消息中添加任何其他字符,因为您的 morseChar
在每个循环中都被重置为空字符串。您需要将变量移出循环以跟踪先前的输入。
此外,is_pressed 是 microbit 文档中的函数。
生成的代码如下所示:
message=''
morseChr=''
while True:
if button_a.is_pressed() and button_b.is_pressed():
# First check if the entered char is actually valid
if morseChr not in morse:
morseChr='' # reset chars to avoid being stuck here endlessly
# maybe also give feedback to the user that the morse code was invalid
continue
# add the char to the message
message += morse[morseChr]
morseChr=''
display.show(message)
sleep(1000*len(message))
display.clear()
elif button_a.is_pressed():
morseChr+='.'
elif button_b.is_pressed():
morseChr+='-'