Apr-07-2019, 01:37 AM
import random # Morse Code string translator # delimited with spaces for characters, and / for words # format example "HELLO WORLD": .... . .-.. .-.. --- / .-- --- .-. .-.. -.. def untranslate_morse_code_string(code_string): InvertmorseAlphabet = { " ": "/", "A": ".-", "C": "-.-.", "B": "-...", "E": ".", "D": "-..", "G": "--.", "F": "..-.", "I": "..", "H": "....", "K": "-.-", "J": ".---", "M": "--", "L": ".-..","O": "---", "N": "-.", "Q": "--.-", "P": ".--.", "S": "...", "R": ".-.", "U": "..-", "T": "-", "W": ".--", "V": "...-", "Y": "-.--", "X": "-..-", "Z": "--..", "1": ".----", "2": "..---", "3": "...--", "4": "....-", "5": ".....", "6": "-....", "7": "--...", "8": "---..", "9": "----.", "0": "-----" } print (InvertmorseAlphabet) print (code_string) def translate_morse_code_string(code_string): morseAlphabet = { "/": " ", ".-": "A", "-.-.": "C", "-...": "B", ".": "E", "-..": "D", "--.": "G", "..-.": "F", "..": "I", "....": "H", "-.-": "K", ".---": "J", "--": "M", ".-..": "L","---": "O", "-.": "N", "--.-": "Q", ".--.": "P", "...": "S", ".-.": "R", "..-": "U", "-": "T", ".--": "W", "...-": "V", "-.--": "Y", "-..-": "X", "--..": "Z", ".----": "1", "..---": "2", "...--": "3", "....-": "4", ".....": "5", "-....": "6", "--...": "7", "---..": "8", "----.": "9", "-----": "0" } Consonant = [ 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'W', 'X', 'Z'] Vowel = ['A', 'E', 'I', 'O', 'U', 'Y'] # Sometimes y and w. Need more vowels Number = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ] # # This below works # input_msg=random.choice(Vowel)+random.choice(Number)+random.choice(Consonant) string_len=len(input_msg) print(input_msg,string_len) # # End of this works # x=input_msg[2] character_output = untranslate_morse_code_string(x) print (x, character_output) # #What happens if I force a particular letter # x='G' character_output = untranslate_morse_code_string(x) print (x, character_output) # #What happens if I use the translate on a particular code # x="..." character_output = translate_morse_code_string(x) print (x, character_output)