from machine import Pin
from time import sleep
led = Pin(0, Pin.OUT)
def dot():
led.value(1)
sleep(0.5)
led.value(0)
sleep(2)
def dash():
led.value(1)
sleep(1)
led.value(0)
sleep(2)
# 0.5 Seconds = Dot
# 1 Second = Dash
# word to be translated
word = "Hi"
Asked
Active
Viewed 293 times
0
-
1Sounds like fun. Are you asking how to convert characters to morse code? – monkut May 18 '22 at 02:04
-
1@monkut Kind of. I just need to know how I would get each separate character from a given word so that I could translate each letter into morse. Or, if you think there's a better approach to it than that, I'd be happy to listen. – Kiri. May 18 '22 at 02:07
-
2You can iterate through a `str` with a `for` loop. – Julien May 18 '22 at 02:15
1 Answers
0
You can treat a string in Python as a list, for example:
word = "Hi"
print(word[0])
print(word[1])
#H
#i
Knowing this, you can iterate over a string:
word = "Hi"
for letter in word:
print(letter)
#H
#i
For your problem, you can also map all the letters with their respective codes:
# 0 -> dot
# 1 -> dash
codes = {
"A": (0, 1),
"B": (1, 0, 0, 0),
"C": (1, 0, 1, 0)
}
word = "AC"
for letter in word:
morse_codes = codes[letter]
print(morse_codes)
}
#(0, 1)
#(1, 0, 1, 0)

mpioski
- 28
- 1
- 6