SHARE
TWEET

Python Morse code converter

a guest Nov 12th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. Morse v1.2
  3. Type in text to have it converted to Morse code.
  4.  
  5. By Steve Shambles Aug 2018, updated Nov 2019.
  6. stevepython.wordpress.com
  7.  
  8. Based on this tutorial:
  9. https://www.python-course.eu/python3_dictionaries.php
  10. """
  11. morse = {
  12.     'A' : '.-',
  13.     'B' : '-...',
  14.     'C' : '-.-.',
  15.     'D' : '-..',
  16.     'E' : '.',
  17.     'F' : '..-.',
  18.     'G' : '--.',
  19.     'H' : '....',
  20.     'I' : '..',
  21.     'J' : '.---',
  22.     'K' : '-.-',
  23.     'L' : '.-..',
  24.     'M' : '--',
  25.     'N' : '-.',
  26.     'O' : '---',
  27.     'P' : '.--.',
  28.     'Q' : '--.-',
  29.     'R' : '.-.',
  30.     'S' : '...',
  31.     'T' : '-',
  32.     'U' : '..-',
  33.     'V' : '...-',
  34.     'W' : '.--',
  35.     'X' : '-..-',
  36.     'Y' : '-.--',
  37.     'Z' : '--..',
  38.     '0' : '-----',
  39.     '1' : '.----',
  40.     '2' : '..---',
  41.     '3' : '...--',
  42.     '4' : '....-',
  43.     '5' : '.....',
  44.     '6' : '-....',
  45.     '7' : '--...',
  46.     '8' : '---..',
  47.     '9' : '----.',
  48.     '.' : '.-.-.-',
  49.     ',' : '--..--',
  50.     ' ' : ' '
  51.     }
  52. # Get user input of a letter\word\phrase.
  53. users_text = input('Input text to convert to Morse code:')
  54.  
  55. # Make uppercase, as in dictionary above.
  56. users_text = users_text.upper()
  57.  
  58. # Split the word into a list of single single_letters.
  59. single_letters = list(users_text)
  60.  
  61. # Print(single_letters) #would look like this,['T', 'W', 'A', 'T']
  62.  
  63. print_this = ''
  64.  
  65. # Iterate through the single_letters list.
  66. for x in single_letters:
  67.     # Store the Morse for each letter from Morse dict into string 'arse'
  68.     # Build new string to print called print_this, containing all the arses
  69.     arse = (morse[x])
  70.     print_this = print_this +arse+' '
  71.  
  72. #print original inputted word and the Morse code for it
  73. print()
  74. print(users_text +' in morse is: '+print_this)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top