Oct-26-2021, 02:26 PM
(Oct-26-2021, 01:46 PM)deanhystad Wrote: The error is saying it tried to lookup "t" in a dictionary and failed. The line where this happened is:
plain_text += morse[current_element]If you look at your code morse only appears in these lines:
morse = {} plain_text += morse[current_element]morse doesn't have any items at all. That is why it crashes when trying to lookup "t".
You should review the code that involves "alphabet". I added a line to print the dictionary.
for key, value in translation_dict.items(): alphabet[key] = key alphabet[value] = value print(alphabet)And I don't think "alphabet" contains what you think it does. A sample...
What is "alphabet" supposed to be? Since it is used in "encode()" I suspect it should be a dictionary of Morse codes for all the letters and numbers. But you already have a dictionary for that; translation_dict. What you don't have is a reverse dictionary for looking up the letter for a Morse code.
Output:{'a': 'a', '.-': '.-', 'b': 'b', '-...': '-...', 'c': 'c', '-.-
So you have a translation dictionary "alphabet" that cannot do translation, and your try to do translation with a dictionary that is empty.
At first I replace
alphabet[key] = value alphabet[value] = keynext I writed
plain_text += alphabet[val]instead
plain_text += valSo now output is
-/..../../...//../...//..-./../.-./.../-//-/./.../-/../-./--.//..././-./-/./-./-.-././.-.-.-///
-/..../../...//../...//..-./../.-./.../-//-/./.../-/../-./--.//..././-./-/./-./-.-././.-.-.-///
-....-his is s.-.-.-cond -....-.-.-.-s-....-ing s.-.-.-n-....-.-.-.-nc.-.-.-.
-....-his is -....-hird -....-.-.-.-s-....-ing s.-.-.-n-....-.-.-.-nc.-.-.-.
-.-/.-//-...///
It is good basis, I see that first two lines and the last one are encoded correctly.
But there is some problem in decoding, some chars are encrypted correctly but most of sentence not...