It would seem that the 21st century, optical fiber, 3 / 4G and other delights of progress – what is Morse code, and even more so converter for Morse code to text, and from text to Morse code, and even in Python? But no, we are against peace π
Most recently, I told my son about the history of communication, and remembered the Morse code. Given that he and his daughter go to robotics courses , as well as a recent conversation, Iβve quickly sprinkled the converter (plans to make an analogue of a Morse code radio station with children on Arduino).
It is built very simply: if – elif, and the converted code is added to a list, which, after processing, is displayed in full.
Important: the code is adapted for converting Morse code into Russian and back. Of course, you can redo the English version, but so lazy;) I hope you understand;)
-
def morse_text():
-
print('q - Π²ΡΡ ΠΎΠ΄')
-
morse_table = []
-
while True:
-
string = str(input('ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΠΊΠΎΠ΄ ΠΠΎΡΠ·Π΅ (1 ΡΠΈΠΌΠ²ΠΎΠ»): '))
-
if string == ('q'):
-
break
-
elif string == '.-':
-
morse_code = ('Π°')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.......':
-
morse_code = (' ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-...':
-
morse_code = ('Π±')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.--':
-
morse_code = ('Π²')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '--.':
-
morse_code = ('Π³')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-..':
-
morse_code = ('Π΄')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.':
-
morse_code = ('Π΅')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '...-':
-
morse_code = ('ΠΆ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '--..β’':
-
morse_code = ('Π·')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '..':
-
morse_code = ('ΠΈ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-.-':
-
morse_code = ('ΠΊ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.-..':
-
morse_code = ('Π»')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '--':
-
morse_code = ('ΠΌ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-.':
-
morse_code = ('Π½')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '---':
-
morse_code = ('ΠΎ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.--.':
-
morse_code = ('ΠΏ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.-.':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '...':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '..-':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '..-.':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '....':
-
morse_code = ('Ρ ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-.-.':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '---':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '----':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '--.-':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '--.--':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-.--':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '..--':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.-.-':
-
morse_code = ('Ρ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.----':
-
morse_code = ('1')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '..---':
-
morse_code = ('2')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '...--':
-
morse_code = ('3')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '....-':
-
morse_code = ('5')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.....':
-
morse_code = ('5')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-....':
-
morse_code = ('6')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '--...':
-
morse_code = ('7')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '---..β’':
-
morse_code = ('8')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '----.':
-
morse_code = ('9')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-----':
-
morse_code = ('0')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '......':
-
morse_code = ('. ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.-.-.-':
-
morse_code = (', ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '---...':
-
morse_code = (':')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-.-.-':
-
morse_code = (';')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-.--.-':
-
morse_code = ('(')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-.--.-':
-
morse_code = (')')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.-..-.':
-
morse_code = ('"')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '..--..':
-
morse_code = ('?')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '--..--':
-
morse_code = ('!')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.--.-.':
-
morse_code = ('@')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '-....-':
-
morse_code = ('-')
-
print(morse_code)
-
morse_table.append(morse_code)
-
elif string == '.......':
-
morse_code = (' ')
-
print(morse_code)
-
morse_table.append(morse_code)
-
-
print(morse_table)
-
morse_table = ''.join(morse_table)
-
print('ΠΠ΄ΠΈΠ½ΠΎΠΉ ΡΡΡΠΎΠΊΠΎΠΉ: ', morse_table)
-
-
def text_morse():
-
text = []
-
text = str(input('ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠ΅ΠΊΡΡ: ')).lower()
-
print(text)
-
morse = []
-
for string in text:
-
if string == 'Π°':
-
morse_code = ('β’ β')
-
print(morse_code)
-
morse.append(morse_code)
-
elif string == 'Π±':
-
morse_code = ('β β’ β’ β’')
-
print('β β’ β’ β’')
-
morse.append(morse_code)
-
elif string == 'Π²':
-
morse_code = ('β’ β β')
-
print('β’ β β')
-
morse.append(morse_code)
-
elif string == 'Π³':
-
morse_code = ('β β β’')
-
print('β β β’')
-
morse.append(morse_code)
-
elif string == 'Π΄':
-
morse_code = ('β β’ β’')
-
print('β β’ β’')
-
morse.append(morse_code)
-
elif string == 'Π΅':
-
morse_code = ('β’ ')
-
print('β’ ')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β’ ')
-
print('β’ ')
-
morse.append(morse_code)
-
elif string == 'ΠΆ':
-
morse_code = ('β’ β’ β’ β')
-
print('β’ β’ β’ β')
-
morse.append(morse_code)
-
elif string == 'Π·':
-
morse_code = ('β β β’ β’')
-
print('β β β’ β’')
-
morse.append(morse_code)
-
elif string == 'ΠΈ':
-
morse_code = ('β’ β’')
-
print('β’ β’')
-
morse.append(morse_code)
-
elif string == 'ΠΊ':
-
morse_code = ('β β’ β')
-
print('β β’ β')
-
morse.append(morse_code)
-
elif string == 'Π»':
-
morse_code = ('β’ β β’ β’')
-
print('β’ β β’ β’')
-
morse.append(morse_code)
-
elif string == 'ΠΌ':
-
morse_code = ('β β')
-
print('β β')
-
morse.append(morse_code)
-
elif string == 'Π½':
-
morse_code = ('β β’')
-
print('β β’')
-
morse.append(morse_code)
-
elif string == 'ΠΎ':
-
morse_code = ('β β β')
-
print('β β β')
-
morse.append(morse_code)
-
elif string == 'ΠΏ':
-
morse_code = ('β’ β β β’')
-
print('β’ β β β’')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β’ β β’')
-
print('β’ β β’')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β’ β’ β’')
-
print('β’ β’ β’')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β')
-
print('β')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β’ β’ β')
-
print('β’ β’ β')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β’ β’ β β’')
-
print('β’ β’ β β’')
-
morse.append(morse_code)
-
elif string == 'Ρ ':
-
morse_code = ('β’ β’ β’ β’')
-
print('β’ β’ β’ β’')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β β’ β β’')
-
print('β β’ β β’')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β β β ')
-
print('β β β β’')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β β β β')
-
print('β β β β')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β β β’ β')
-
print('β β β’ β')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β β β’ β β')
-
print('β β β’ β β')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β β’ β β')
-
print('β β’ β β')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β β’ β’ β')
-
print('β β’ β’ β')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β’ β’ β β')
-
print('β’ β’ β β')
-
morse.append(morse_code)
-
elif string == 'Ρ':
-
morse_code = ('β’ β β’ β')
-
print('β’ β β’ β')
-
morse.append(morse_code)
-
elif string == '1':
-
morse_code = ('β’ β β β β')
-
print('β’ β β β β')
-
morse.append(morse_code)
-
elif string == '2':
-
morse_code = ('β’ β’ β β β')
-
print('β’ β’ β β β')
-
morse.append(morse_code)
-
elif string == '3':
-
morse_code = ('β’ β’ β’ β β')
-
print('β’ β’ β’ β β')
-
morse.append(morse_code)
-
elif string == '4':
-
morse_code = ('β’ β’ β’ β’ β')
-
print('β’ β’ β’ β’ β')
-
morse.append(morse_code)
-
elif string == '5':
-
morse_code = ('β’ β’ β’ β’ β’')
-
print('β’ β’ β’ β’ β’')
-
morse.append(morse_code)
-
elif string == '6':
-
morse_code = ('β β’ β’ β’ β’')
-
print('β β’ β’ β’ β’')
-
morse.append(morse_code)
-
elif string == '7':
-
morse_code = ('β β β’ β’ β’')
-
print('β β β’ β’ β’')
-
morse.append(morse_code)
-
elif string == '8':
-
morse_code = ('β β β β’ β’')
-
print('β β β β’ β’')
-
morse.append(morse_code)
-
elif string == '9':
-
morse_code = ('β β β β β’')
-
print('β β β β β’')
-
morse.append(morse_code)
-
elif string == '0':
-
morse_code = ('β β β β β')
-
print('β β β β β')
-
morse.append(morse_code)
-
elif string == '.':
-
morse_code = ('β’ β’ β’ β’ β’ β’')
-
print('β’ β’ β’ β’ β’ β’')
-
morse.append(morse_code)
-
elif string == ',':
-
morse_code = ('β’ β β’ β β’ β')
-
print('β’ β β’ β β’ β')
-
morse.append(morse_code)
-
elif string == ':':
-
morse_code = ('β β β β’ β’ β’')
-
print('β β β β’ β’ β’')
-
morse.append(morse_code)
-
elif string == ';':
-
morse_code = ('β β’ β β’ β')
-
print('β β’ β β’ β')
-
morse.append(morse_code)
-
elif string == '(':
-
morse_code = ('β β’ β β β’ β')
-
print('β β’ β β β’ β')
-
morse.append(morse_code)
-
elif string == ')':
-
morse_code = ('β β’ β β β’ β')
-
print('β β’ β β β’ β')
-
morse.append(morse_code)
-
elif string == '"':
-
morse_code = ('β’ β β’ β’ β β’')
-
print('β’ β β’ β’ β β’')
-
morse.append(morse_code)
-
elif string == '?':
-
morse_code = ('β’ β’ β β β’ β’')
-
print('β’ β’ β β β’ β’')
-
morse.append(morse_code)
-
elif string == '!':
-
morse_code = ('β β β’ β’ β β')
-
print('β β β’ β’ β β')
-
morse.append(morse_code)
-
elif string == '@':
-
morse_code = ('β’ β β β’ β β’')
-
print('β’ β β β’ β β’')
-
morse.append(morse_code)
-
elif string == '-':
-
morse_code = ('β β’ β’ β’ β’ β')
-
print('β β’ β’ β’ β’ β')
-
morse.append(morse_code)
-
elif string == ' ':
-
morse_code = ('.......')
-
print('.......')
-
morse.append(morse_code)
-
-
morse = ''.join(morse)
-
print('ΠΠ΄ΠΈΠ½ΠΎΠΉ ΡΡΡΠΎΠΊΠΎΠΉ: ', morse)
-
-
def choice():
-
choice = str(input(' ΠΠ· ΡΠ΅ΠΊΡΡΠ° Π² ΠΊΠΎΠ΄ ΠΠΎΡΠ·Π΅ - 1\n ΠΠ· ΠΊΠΎΠ΄Π° ΠΠΎΡΠ·Π΅ Π² ΡΠ΅ΠΊΡΡ - 2\n'))
-
if choice == '1':
-
text_morse()
-
elif choice == '2':
-
morse_text()
-
-
choice()
The code itself is available for download at the link .
In case of questions – please write to the mail.
Yes, by the way, I feel that I will soon be preparing a series of materials on Arduino π But this is not yet accurate … π Thank you for your attention! And do not forget to share a link to a post on social networks.