Oct-25-2021, 08:39 PM
Hello I would like to encode text to the Morse alphabet.
I need to debug it to correct outputs for the last four lines of print.
The ones on lines 31, 32 and 33 are first encoded and then decoded, so output should be the same as in the code.
The output on line 34 should be -.- / .- // -… ///
But in output I see:
Traceback (most recent call last):
File "C:/Users/dokipo/Desktop/pythonprojects/alphabet.py", line 29, in <module>
print(decode(encode("This is first testing sentence.")))
File "C:/Users/dokipo/Desktop/pythonprojects/alphabet.py", line 21, in <module>
morse_array = morse_text.split("/")
builtins.NameError: name 'morse_text' is not defined
Do you know how to debug this code? I need to let it in this form generally but improve bugs.
Thank you
I need to debug it to correct outputs for the last four lines of print.
The ones on lines 31, 32 and 33 are first encoded and then decoded, so output should be the same as in the code.
The output on line 34 should be -.- / .- // -… ///
But in output I see:
Traceback (most recent call last):
File "C:/Users/dokipo/Desktop/pythonprojects/alphabet.py", line 29, in <module>
print(decode(encode("This is first testing sentence.")))
File "C:/Users/dokipo/Desktop/pythonprojects/alphabet.py", line 21, in <module>
morse_array = morse_text.split("/")
builtins.NameError: name 'morse_text' is not defined
Do you know how to debug this code? I need to let it in this form generally but improve bugs.
Thank you
import string translation_dict = dict([('a', '.-'), ('b', '-...'), ('c', '-.-.'), ('d', '-..'), ('e', '.'), ('f', '..-.'), ('g', '--.'), ('h', '....'), ('i', '..'), ('j', '.---'), ('k', '-.-'), ('l', '.-..'), ('m', '--'), ('n', '-.'), ('o', '---'), ('p', '.--.'), ('q', '--.-'), ('r', '.-.'), ('s', '...'), ('t','-'),('u', '..-'), ('v', '...-'), ('w', '.--'), ('x', '-..-'), ('y', '-.--'), ('z', '--..'), (' ', ''), ('/', '-..-.'), ('-', '-....-'), ('.', '.-.-.-'), (',', '--..--'), (' ', ''), ("1", ".----"), ("2", "..---"), ("3", "...--"), ("4", "....-"), ("5", "....."), ("6", "-...."), ("7", "--..."), ("8", "---.."), ("9", "----."), ("0", "-----")]) alphabet = {} morse = {} for key, value in translation_dict.items(): alphabet[key] = key alphabet[value] = value def encode(plaintext: str) -> str: plaintext = plaintext.lower() morse_text = "" for char in plaintext: morse_text += alphabet[char] + "/" morse_text += "//" return morse_text def decode(mroz_text: str) -> str: morse_array = morse_text.split("/") plain_text = "" for i in range(len(morse_array)): current_element = morse_array[i] plain_text += morse[current_element] plain_text = plain_text[:-5] return plain_text print(decode(encode("This is first testing sentence."))) print(decode(encode("This is second testing sentence."))) print(decode(encode("This is third testing sentence."))) print(encode("ka b")) # -.-/.-//-...///