Python Forum
How to fix bugs in Morse alphabet code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fix bugs in Morse alphabet code?
#8
(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...
Output:
{'a': 'a', '.-': '.-', 'b': 'b', '-...': '-...', 'c': 'c', '-.-
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.
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] = key
next I writed

plain_text += alphabet[val]
instead
plain_text += val
So 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...
Reply


Messages In This Thread
How to fix bugs in Morse alphabet code? - by dokipo - Oct-25-2021, 08:39 PM
RE: How to fix bugs in Morse alphabet code? - by dokipo - Oct-26-2021, 02:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,715 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  reset on inactivity (building a morse decoder) gerrit1985 7 3,683 Apr-17-2020, 10:22 AM
Last Post: deanhystad
  How do you switch all alphabet into different characters? SteampunkMaverick12 1 2,203 Apr-27-2018, 12:09 AM
Last Post: ODIS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020