Search code examples
pythonglobal-variables

using global variables to encode and decode morse using a list of morsecode


Basically I have to make a morse code encoder and decoder using two functions. My professor's template (and of course I have added code to the first function and main function) includes a global variable and I have no idea how to use it. Also I am getting an error for the current code because the letter is somehow an integer not a string letter. Can someone just explain how I could use the global variable in this program, because I am not worried about the errors right now?

MORSE_CODES=[' .- ',' -... ',' -.-. ',' -.. ',' . ',' ..-. ',' --. ',' .... ',' .. ',
' .--- ',' -.- ',' .-.. ',' -- ',' -. ',' --- ',' .--. ',' --.- ',' .-. ',
' ... ',' - ',' ..- ',' ...- ',' .-- ',' -..- ',' -.-- ',' --.. ']

ASCII_A=65 #global var




def encode_Morse(my_msg):
  my_msg_Morse=(my_msg.upper())
  my_msg_Morse=my_msg_Morse.replace(" ","   ")
 
  
  for letter in range(0, len(my_msg_Morse)):
    
    if ord(letter)>=65 and ord(letter)<=90:
      my_msg_Morse=my_msg_Morse.replace(letter, MORSE_CODES[letter])
    else:
      my_msg_Morse=my_msg_Morse.replace(letter,'*')
    my_msg_Morse=my_msg_Morse+" "+letter
  return my_msg_Morse

**#def decode_Morse(my_msg):
my_msg_Morse=my_msg.split("    ")
  string=""
  for word in my_msg_Morse:**


def main():
  my_msg=input("Enter your message: ")
  my_msg=my_msg.upper()
  print("Your message in plain text: ", my_msg)
  
  my_msg_Morse=encode_Morse(my_msg)
  print("Your message in Morse Code: ", my_msg_Morse)

Solution

  • Issues

    1. for letter in range(0, len(my_msg_Morse)): does not loop over letters in message
    2. my_msg_Morse=my_msg_Morse.replace(letter, MORSE_CODES[letter]) causes replacement of Morse code letters in messages with spaces (i.e. multiple word messages)
    3. Regarding globals -- just use them if you only need to use their values (which you are here)

    Code

    MORSE_CODES=[' .- ',' -... ',' -.-. ',' -.. ',' . ',' ..-. ',' --. ',' .... ',' .. ',
    ' .--- ',' -.- ',' .-.. ',' -- ',' -. ',' --- ',' .--. ',' --.- ',' .-. ',
    ' ... ',' - ',' ..- ',' ...- ',' .-- ',' -..- ',' -.-- ',' --.. ']
    
    ASCII_A=65 #global var
    
    def encode_Morse(my_msg):
      result = ''
      for letter in my_msg_Morse:
        if 65 <= ord(letter) <= 90:
          if not result:
              result = MORSE_CODES[ord(letter)-ASCII_A]          # first Morse character
          else:
              result += ' ' + MORSE_CODES[ord(letter)-ASCII_A] # space between Morse character
        else:
          result += '*' 
        
      return result
    
    def decode_Morse(my_msg):
        for i, code in enumerate(MORSE_CODES):
            my_msg = my_msg.replace(code, chr(i+65))
      
        return my_msg.replace('*', ' ')
    
    def decode_Morse_using_counter(my_msg):
        ' Version using counter rather than enumerate to track index '
        i = 0
        for code in MORSE_CODES:
            my_msg = my_msg.replace(code, chr(i+65))
            i += 1
      
        return my_msg.replace('*', ' ')
    
    def main():
      my_msg = input("Enter your message: ")
      my_msg = my_msg.upper().replace(' ', '   ')  # upper case with 3 letters between words
      print("Your message in plain text: ", my_msg)
      encode_msg = encode_Morse(my_msg)
      print("Your message in Morse Code: ", encode_msg)
      print("Decoded Morse Code: ", decode_Morse(encode_msg))
    
    main()
    

    Example Run

    Enter your message: This is a simple message
    Your message in plain text:  THIS   IS   A   SIMPLE   MESSAGE
    Your message in Morse Code:   -  ....  ..  ... *** ..  ... *** .- *** ...  ..  --  .--.  .-..  . *** --  .  ...  ...  .-  --.  . 
    Decoded Morse Code:  THIS   IS   A   SIMPLE   MESSAGE
    

    Function Rewrite

    Using list comprehension and Walrus operator, the encode_Morse function can be reduced to:

    def encode_Morse(my_msg):
      my_msg_Morse = my_msg.upper()
      return ''.join(MORSE_CODES[p-ASCII_A] if 65 <=(p:=ord(letter))<= 90 else '*' for letter in my_msg_Morse)