Python – Morse code converter and back


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;)

  1. def morse_text():
  2.     print('q - Π²Ρ‹Ρ…ΠΎΠ΄')
  3.     morse_table = []
  4.     while True:
  5.         string = str(input('Π’Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΊΠΎΠ΄ ΠœΠΎΡ€Π·Π΅ (1 символ): '))
  6.         if string == ('q'):
  7.             break
  8.         elif string == '.-':
  9.             morse_code = ('Π°')
  10.             print(morse_code)
  11.             morse_table.append(morse_code)
  12.         elif string == '.......':
  13.             morse_code = (' ')
  14.             print(morse_code)
  15.             morse_table.append(morse_code)
  16.         elif string == '-...':
  17.             morse_code = ('Π±')
  18.             print(morse_code)
  19.             morse_table.append(morse_code)
  20.         elif string == '.--':
  21.             morse_code = ('Π²')
  22.             print(morse_code)
  23.             morse_table.append(morse_code)
  24.         elif string == '--.':
  25.             morse_code = ('Π³')
  26.             print(morse_code)
  27.             morse_table.append(morse_code)
  28.         elif string == '-..':
  29.             morse_code = ('Π΄')
  30.             print(morse_code)
  31.             morse_table.append(morse_code)
  32.         elif string == '.':
  33.             morse_code = ('Π΅')
  34.             print(morse_code)
  35.             morse_table.append(morse_code)
  36.         elif string == '...-':
  37.             morse_code = ('ΠΆ')
  38.             print(morse_code)
  39.             morse_table.append(morse_code)
  40.         elif string == '--..β€’':
  41.             morse_code = ('Π·')
  42.             print(morse_code)
  43.             morse_table.append(morse_code)
  44.         elif string == '..':
  45.             morse_code = ('ΠΈ')
  46.             print(morse_code)
  47.             morse_table.append(morse_code)
  48.         elif string == '-.-':
  49.             morse_code = ('ΠΊ')
  50.             print(morse_code)
  51.             morse_table.append(morse_code)
  52.         elif string == '.-..':
  53.             morse_code = ('Π»')
  54.             print(morse_code)
  55.             morse_table.append(morse_code)
  56.         elif string == '--':
  57.             morse_code = ('ΠΌ')
  58.             print(morse_code)
  59.             morse_table.append(morse_code)
  60.         elif string == '-.':
  61.             morse_code = ('Π½')
  62.             print(morse_code)
  63.             morse_table.append(morse_code)
  64.         elif string == '---':
  65.             morse_code = ('ΠΎ')
  66.             print(morse_code)
  67.             morse_table.append(morse_code)
  68.         elif string == '.--.':
  69.             morse_code = ('ΠΏ')
  70.             print(morse_code)
  71.             morse_table.append(morse_code)
  72.         elif string == '.-.':
  73.             morse_code = ('Ρ€')
  74.             print(morse_code)
  75.             morse_table.append(morse_code)
  76.         elif string == '...':
  77.             morse_code = ('с')
  78.             print(morse_code)
  79.             morse_table.append(morse_code)
  80.         elif string == '-':
  81.             morse_code = ('Ρ‚')
  82.             print(morse_code)
  83.             morse_table.append(morse_code)
  84.         elif string == '..-':
  85.             morse_code = ('Ρƒ')
  86.             print(morse_code)
  87.             morse_table.append(morse_code)
  88.         elif string == '..-.':
  89.             morse_code = ('Ρ„')
  90.             print(morse_code)
  91.             morse_table.append(morse_code)
  92.         elif string == '....':
  93.             morse_code = ('Ρ…')
  94.             print(morse_code)
  95.             morse_table.append(morse_code)
  96.         elif string == '-.-.':
  97.             morse_code = ('Ρ†')
  98.             print(morse_code)
  99.             morse_table.append(morse_code)
  100.         elif string == '---':
  101.             morse_code = ('Ρ†')
  102.             print(morse_code)
  103.             morse_table.append(morse_code)
  104.         elif string == '----':
  105.             morse_code = ('ш')
  106.             print(morse_code)
  107.             morse_table.append(morse_code)
  108.         elif string == '--.-':
  109.             morse_code = ('Ρ‰')
  110.             print(morse_code)
  111.             morse_table.append(morse_code)
  112.         elif string == '--.--':
  113.             morse_code = ('ъ')
  114.             print(morse_code)
  115.             morse_table.append(morse_code)
  116.         elif string == '-.--':
  117.             morse_code = ('Ρ‹')
  118.             print(morse_code)
  119.             morse_table.append(morse_code)
  120.         elif string == '..--':
  121.             morse_code = ('ю')
  122.             print(morse_code)
  123.             morse_table.append(morse_code)
  124.         elif string == '.-.-':
  125.             morse_code = ('я')
  126.             print(morse_code)
  127.             morse_table.append(morse_code)
  128.         elif string == '.----':
  129.             morse_code = ('1')
  130.             print(morse_code)
  131.             morse_table.append(morse_code)
  132.         elif string == '..---':
  133.             morse_code = ('2')
  134.             print(morse_code)
  135.             morse_table.append(morse_code)
  136.         elif string == '...--':
  137.             morse_code = ('3')
  138.             print(morse_code)
  139.             morse_table.append(morse_code)
  140.         elif string == '....-':
  141.             morse_code = ('5')
  142.             print(morse_code)
  143.             morse_table.append(morse_code)
  144.         elif string == '.....':
  145.             morse_code = ('5')
  146.             print(morse_code)
  147.             morse_table.append(morse_code)
  148.         elif string == '-....':
  149.             morse_code = ('6')
  150.             print(morse_code)
  151.             morse_table.append(morse_code)
  152.         elif string == '--...':
  153.             morse_code = ('7')
  154.             print(morse_code)
  155.             morse_table.append(morse_code)
  156.         elif string == '---..β€’':
  157.             morse_code = ('8')
  158.             print(morse_code)
  159.             morse_table.append(morse_code)
  160.         elif string == '----.':
  161.             morse_code = ('9')
  162.             print(morse_code)
  163.             morse_table.append(morse_code)
  164.         elif string == '-----':
  165.             morse_code = ('0')
  166.             print(morse_code)
  167.             morse_table.append(morse_code)
  168.         elif string == '......':
  169.             morse_code = ('. ')
  170.             print(morse_code)
  171.             morse_table.append(morse_code)
  172.         elif string == '.-.-.-':
  173.             morse_code = (', ')
  174.             print(morse_code)
  175.             morse_table.append(morse_code)
  176.         elif string == '---...':
  177.             morse_code = (':')
  178.             print(morse_code)
  179.             morse_table.append(morse_code)
  180.         elif string == '-.-.-':
  181.             morse_code = (';')
  182.             print(morse_code)
  183.             morse_table.append(morse_code)
  184.         elif string == '-.--.-':
  185.             morse_code = ('(')
  186.             print(morse_code)
  187.             morse_table.append(morse_code)
  188.         elif string == '-.--.-':
  189.             morse_code = (')')
  190.             print(morse_code)
  191.             morse_table.append(morse_code)
  192.         elif string == '.-..-.':
  193.             morse_code = ('"')
  194.             print(morse_code)
  195.             morse_table.append(morse_code)
  196.         elif string == '..--..':
  197.             morse_code = ('?')
  198.             print(morse_code)
  199.             morse_table.append(morse_code)
  200.         elif string == '--..--':
  201.             morse_code = ('!')
  202.             print(morse_code)
  203.             morse_table.append(morse_code)
  204.         elif string == '.--.-.':
  205.             morse_code = ('@')
  206.             print(morse_code)
  207.             morse_table.append(morse_code)
  208.         elif string == '-....-':
  209.             morse_code = ('-')
  210.             print(morse_code)
  211.             morse_table.append(morse_code)
  212.         elif string == '.......':
  213.             morse_code = (' ')
  214.             print(morse_code)
  215.             morse_table.append(morse_code)
  216.  
  217.     print(morse_table)
  218.     morse_table = ''.join(morse_table)
  219.     print('Π•Π΄ΠΈΠ½ΠΎΠΉ строкой: ', morse_table)
  220.  
  221. def text_morse():
  222.     text = []
  223.     text = str(input('Π’Π²Π΅Π΄ΠΈΡ‚Π΅ тСкст: ')).lower()
  224.     print(text)
  225.     morse = []
  226.     for string in text:
  227.         if string == 'Π°':
  228.             morse_code = ('β€’ β€”')
  229.             print(morse_code)
  230.             morse.append(morse_code)
  231.         elif string == 'Π±':
  232.             morse_code = ('β€” β€’ β€’ β€’')
  233.             print('β€” β€’ β€’ β€’')
  234.             morse.append(morse_code)
  235.         elif string == 'Π²':
  236.             morse_code = ('β€’ β€” β€”')
  237.             print('β€’ β€” β€”')
  238.             morse.append(morse_code)
  239.         elif string == 'Π³':
  240.             morse_code = ('β€” β€” β€’')
  241.             print('β€” β€” β€’')
  242.             morse.append(morse_code)
  243.         elif string == 'Π΄':
  244.             morse_code = ('β€” β€’ β€’')
  245.             print('β€” β€’ β€’')
  246.             morse.append(morse_code)
  247.         elif string == 'Π΅':
  248.             morse_code = ('β€’ ')
  249.             print('β€’ ')
  250.             morse.append(morse_code)
  251.         elif string == 'Ρ‘':
  252.             morse_code = ('β€’ ')
  253.             print('β€’ ')
  254.             morse.append(morse_code)
  255.         elif string == 'ΠΆ':
  256.             morse_code = ('β€’ β€’ β€’ β€”')
  257.             print('β€’ β€’ β€’ β€”')
  258.             morse.append(morse_code)
  259.         elif string == 'Π·':
  260.             morse_code = ('β€” β€” β€’ β€’')
  261.             print('β€” β€” β€’ β€’')
  262.             morse.append(morse_code)
  263.         elif string == 'ΠΈ':
  264.             morse_code = ('β€’ β€’')
  265.             print('β€’ β€’')
  266.             morse.append(morse_code)
  267.         elif string == 'ΠΊ':
  268.             morse_code = ('β€” β€’ β€”')
  269.             print('β€” β€’ β€”')
  270.             morse.append(morse_code)
  271.         elif string == 'Π»':
  272.             morse_code = ('β€’ β€” β€’ β€’')
  273.             print('β€’ β€” β€’ β€’')
  274.             morse.append(morse_code)
  275.         elif string == 'ΠΌ':
  276.             morse_code = ('β€” β€”')
  277.             print('β€” β€”')
  278.             morse.append(morse_code)
  279.         elif string == 'Π½':
  280.             morse_code = ('β€” β€’')
  281.             print('β€” β€’')
  282.             morse.append(morse_code)
  283.         elif string == 'ΠΎ':
  284.             morse_code = ('β€” β€” β€”')
  285.             print('β€” β€” β€”')
  286.             morse.append(morse_code)
  287.         elif string == 'ΠΏ':
  288.             morse_code = ('β€’ β€” β€” β€’')
  289.             print('β€’ β€” β€” β€’')
  290.             morse.append(morse_code)
  291.         elif string == 'Ρ€':
  292.             morse_code = ('β€’ β€” β€’')
  293.             print('β€’ β€” β€’')
  294.             morse.append(morse_code)
  295.         elif string == 'с':
  296.             morse_code = ('β€’ β€’ β€’')
  297.             print('β€’ β€’ β€’')
  298.             morse.append(morse_code)
  299.         elif string == 'Ρ‚':
  300.             morse_code = ('β€”')
  301.             print('β€”')
  302.             morse.append(morse_code)
  303.         elif string == 'Ρƒ':
  304.             morse_code = ('β€’ β€’ β€”')
  305.             print('β€’ β€’ β€”')
  306.             morse.append(morse_code)
  307.         elif string == 'Ρ„':
  308.             morse_code = ('β€’ β€’ β€” β€’')
  309.             print('β€’ β€’ β€” β€’')
  310.             morse.append(morse_code)
  311.         elif string == 'Ρ…':
  312.             morse_code = ('β€’ β€’ β€’ β€’')
  313.             print('β€’ β€’ β€’ β€’')
  314.             morse.append(morse_code)
  315.         elif string == 'Ρ†':
  316.             morse_code = ('β€” β€’ β€” β€’')
  317.             print('β€” β€’ β€” β€’')
  318.             morse.append(morse_code)
  319.         elif string == 'Ρ‡':
  320.             morse_code = ('β€” β€” β€” ')
  321.             print('β€” β€” β€” β€’')
  322.             morse.append(morse_code)
  323.         elif string == 'ш':
  324.             morse_code = ('β€” β€” β€” β€”')
  325.             print('β€” β€” β€” β€”')
  326.             morse.append(morse_code)
  327.         elif string == 'Ρ‰':
  328.             morse_code = ('β€” β€” β€’ β€”')
  329.             print('β€” β€” β€’ β€”')
  330.             morse.append(morse_code)
  331.         elif string == 'ъ':
  332.             morse_code = ('β€” β€” β€’ β€” β€”')
  333.             print('β€” β€” β€’ β€” β€”')
  334.             morse.append(morse_code)
  335.         elif string == 'Ρ‹':
  336.             morse_code = ('β€” β€’ β€” β€”')
  337.             print('β€” β€’ β€” β€”')
  338.             morse.append(morse_code)
  339.         elif string == 'ь':
  340.             morse_code = ('β€” β€’ β€’ β€”')
  341.             print('β€” β€’ β€’ β€”')
  342.             morse.append(morse_code)
  343.         elif string == 'ю':
  344.             morse_code = ('β€’ β€’ β€” β€”')
  345.             print('β€’ β€’ β€” β€”')
  346.             morse.append(morse_code)
  347.         elif string == 'я':
  348.             morse_code = ('β€’ β€” β€’ β€”')
  349.             print('β€’ β€” β€’ β€”')
  350.             morse.append(morse_code)
  351.         elif string == '1':
  352.             morse_code = ('β€’ β€” β€” β€” β€”')
  353.             print('β€’ β€” β€” β€” β€”')
  354.             morse.append(morse_code)
  355.         elif string == '2':
  356.             morse_code = ('β€’ β€’ β€” β€” β€”')
  357.             print('β€’ β€’ β€” β€” β€”')
  358.             morse.append(morse_code)
  359.         elif string == '3':
  360.             morse_code = ('β€’ β€’ β€’ β€” β€”')
  361.             print('β€’ β€’ β€’ β€” β€”')
  362.             morse.append(morse_code)
  363.         elif string == '4':
  364.             morse_code = ('β€’ β€’ β€’ β€’ β€”')
  365.             print('β€’ β€’ β€’ β€’ β€”')
  366.             morse.append(morse_code)
  367.         elif string == '5':
  368.             morse_code = ('β€’ β€’ β€’ β€’ β€’')
  369.             print('β€’ β€’ β€’ β€’ β€’')
  370.             morse.append(morse_code)
  371.         elif string == '6':
  372.             morse_code = ('β€” β€’ β€’ β€’ β€’')
  373.             print('β€” β€’ β€’ β€’ β€’')
  374.             morse.append(morse_code)
  375.         elif string == '7':
  376.             morse_code = ('β€” β€” β€’ β€’ β€’')
  377.             print('β€” β€” β€’ β€’ β€’')
  378.             morse.append(morse_code)
  379.         elif string == '8':
  380.             morse_code = ('β€” β€” β€” β€’ β€’')
  381.             print('β€” β€” β€” β€’ β€’')
  382.             morse.append(morse_code)
  383.         elif string == '9':
  384.             morse_code = ('β€” β€” β€” β€” β€’')
  385.             print('β€” β€” β€” β€” β€’')
  386.             morse.append(morse_code)
  387.         elif string == '0':
  388.             morse_code = ('β€” β€” β€” β€” β€”')
  389.             print('β€” β€” β€” β€” β€”')
  390.             morse.append(morse_code)
  391.         elif string == '.':
  392.             morse_code = ('β€’ β€’ β€’ β€’ β€’ β€’')
  393.             print('β€’ β€’ β€’ β€’ β€’ β€’')
  394.             morse.append(morse_code)
  395.         elif string == ',':
  396.             morse_code = ('β€’ β€” β€’ β€” β€’ β€”')
  397.             print('β€’ β€” β€’ β€” β€’ β€”')
  398.             morse.append(morse_code)
  399.         elif string == ':':
  400.             morse_code = ('β€” β€” β€” β€’ β€’ β€’')
  401.             print('β€” β€” β€” β€’ β€’ β€’')
  402.             morse.append(morse_code)
  403.         elif string == ';':
  404.             morse_code = ('β€” β€’ β€” β€’ β€”')
  405.             print('β€” β€’ β€” β€’ β€”')
  406.             morse.append(morse_code)
  407.         elif string == '(':
  408.             morse_code = ('β€” β€’ β€” β€” β€’ β€”')
  409.             print('β€” β€’ β€” β€” β€’ β€”')
  410.             morse.append(morse_code)
  411.         elif string == ')':
  412.             morse_code = ('β€” β€’ β€” β€” β€’ β€”')
  413.             print('β€” β€’ β€” β€” β€’ β€”')
  414.             morse.append(morse_code)
  415.         elif string == '"':
  416.             morse_code = ('β€’ β€” β€’ β€’ β€” β€’')
  417.             print('β€’ β€” β€’ β€’ β€” β€’')
  418.             morse.append(morse_code)
  419.         elif string == '?':
  420.             morse_code = ('β€’ β€’ β€” β€” β€’ β€’')
  421.             print('β€’ β€’ β€” β€” β€’ β€’')
  422.             morse.append(morse_code)
  423.         elif string == '!':
  424.             morse_code = ('β€” β€” β€’ β€’ β€” β€”')
  425.             print('β€” β€” β€’ β€’ β€” β€”')
  426.             morse.append(morse_code)
  427.         elif string == '@':
  428.             morse_code = ('β€’ β€” β€” β€’ β€” β€’')
  429.             print('β€’ β€” β€” β€’ β€” β€’')
  430.             morse.append(morse_code)
  431.         elif string == '-':
  432.             morse_code = ('β€” β€’ β€’ β€’ β€’ β€”')
  433.             print('β€” β€’ β€’ β€’ β€’ β€”')
  434.             morse.append(morse_code)
  435.         elif string == ' ':
  436.             morse_code = ('.......')
  437.             print('.......')
  438.             morse.append(morse_code)
  439.  
  440.     morse = ''.join(morse)
  441.     print('Π•Π΄ΠΈΠ½ΠΎΠΉ строкой: ', morse)
  442.  
  443. def choice():
  444.     choice = str(input(' Из тСкста Π² ΠΊΠΎΠ΄ ΠœΠΎΡ€Π·Π΅ - 1\n Из ΠΊΠΎΠ΄Π° ΠœΠΎΡ€Π·Π΅ Π² тСкст - 2\n'))
  445.     if choice == '1':
  446.         text_morse()
  447.     elif choice == '2':
  448.         morse_text()
  449.  
  450. 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.