Reputation: 55
I am making a morse code translator, and I would like to have a list that is easier to make sure the morse is matched right.
here is the current code:
english = [ "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p", # list all translateable english characters
"q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6",
"7","8","9","0",".",",",";",":","!","?","(",")","-","_","&","=",
"+","$","/","'"," ", "", "",'"']
morse = [ ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-", # list all translateable morse code characters, in same order
".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
".--","-..-","-.--","--..",".----","..---","...--","....-",".....",
"-....","--...","---..","----.","-----",".-.-.-","--..--","-.-.-.",
"---...","-.-.--","..--..","-.--.","-.--.-","-....-","..--.-",
".-...","-...-",".-.-.","...-..-","-..-.",".----.","/", " ", "", ".-..-."]
morselesson = '''Morse code is a telecommunications language made of dots,
dashes, spaces, and slashes. The letters are comprised of
dots and dashes. Spaces are represented with slashes, and
gaps between letters marked with spaces. '''
print("\nNote: not all characters are logged. \n\n")
print("Enter 'code553127' for a brief lesson about morse code. \n")
print('''Otherwise, type your morse code or english and it will be
translated to the other language. ''')
while True:
tobetranslated = input("\nTranslate\n\n> ").lower() # get input, make all letter cases the same
if tobetranslated == "what is morse code":
print(morselesson)
else:
try: #morse to english
tobetranslated = tobetranslated.replace(" ", "π") # change spaces between letters into split markers (untypeable character)
tobetranslated = tobetranslated.replace(" / ", "π/π") # change morse spaces into english spaces and an untypeable character
splitupinput = list(tobetranslated.split("π")) # split along the untypeable characters, leaving spaces, and splits
finishedoutput = "" # create variable for the end output, to be modified
for i in splitupinput: # "for every character in this string:"
if i in splitupinput: # IF inputted character is valid:
morselocation = morse.index(i.strip()) # find location of input morse coded text in the morse code list
finishedoutput = finishedoutput + english[morselocation] # add segment of translated text to text, separation is already there
except: #english to morse
tobetranslated = tobetranslated.replace("π", " ") # undoing what "try" did
tobetranslated = tobetranslated.replace("π/π", " / ")
splitupinput = list(tobetranslated) # divide input into individual characters to be translated
finishedoutput = "" # create variable for the end output, to be modified
for i in splitupinput: # "for every character in this string:"
if i in english: # IF inputted character is valid:
englishlocation = english.index(i) # Find location of input character in English character list
finishedoutput = finishedoutput + morse[englishlocation] + " " # Add segment of translated text to output, and add separation
print("\n"+finishedoutput+"\n") # when done with translating, display translated text
So right now, I have 2 lists, and the locations are matched, but this makes troubleshooting incorrect translations hard. So I want to make an [("a" = "b"), ("c" = "d")]
situation, where afterward, I can create something that lets me get "b" from "a", or "a" from "b".
I want a list, not a huge list of variables.
My current situation, broken down quickly:
Does it work in morse code?
Try:
chop sentence apart at the spaces (which separate letters, slashes separate words)
Find segment in the Morse Code list, and find location
find the location in English list
Add the English version of segment to the output
except:
if it doesn't work and there is error:
chop it up into every single character
find location of character in English list
find translated version (same location) in Morse Code list
Add morse code version of segment to output
print output
I don't know if that was helpful in any way (you could have looked at my excessive comments (from when it was a jumble of bad code, to organize it), but I would like some help
I just have the standard library, no other modules and whatnot (strict parents, dad says he is 'thinking about it') so if you could just use the standard library that would be nice
Upvotes: 1
Views: 129
Reputation: 51
You could do something like
match = dict(zip(english, morse))
to combine all the English letters to morse in a dictionary
Upvotes: 5