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?
#4
import string

alphabet = {}
morse = {}


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", "-----")])



for key, value in translation_dict.items():
    alphabet[key] = key
    alphabet[value] = value

def encode(plaintext):
    morse_text = ""
    plaintext = plaintext.lower()
    for char in plaintext:
        morse_text += alphabet[char] + "/"
    morse_text += "//"
    return morse_text

def decode(mroz_text):
    plain_text = ""
    morse_array = mroz_text.split("/")
    for val in morse_array:
        plain_text += val
    return plain_text


print(encode("This is first testing sentence.")) # for testing
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")) # -.-/.-//-...///
Output:
t/h/i/s/ /i/s/ /f/i/r/s/t/ /t/e/s/t/i/n/g/ /s/e/n/t/e/n/c/e/./// this is first testing sentence. this is second testing sentence. this is third testing sentence. k/a/ /b///
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 Axel_Erfurt - Oct-25-2021, 09:31 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,661 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  reset on inactivity (building a morse decoder) gerrit1985 7 3,592 Apr-17-2020, 10:22 AM
Last Post: deanhystad
  How do you switch all alphabet into different characters? SteampunkMaverick12 1 2,177 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