Morse Code problems

python

#1


So below is my code, I am trying to write a morse code program, I want it to print the first value of these variables if the second value is detected. I have not been writing in 9 months anything and I honestly did not know much about python, I only finished a course on codecademy, so I am just gonna ask a few things. First of all - return needs to be inside of a function, but when input is there also then when I am running the code it is not printing the sentence inside of raw_input. Next - I do not really know how to make it happen not only to single letters - if I print something with capital letters it just brings up error, only works with single letters, and not works like A B C - it also makes error, do not know how to change it to work. I know that I can change it to the point it will not matter if the letter is capital or normal, but I want to bring it to work right now, not work on not so important things at the moment. Will accept any comments, I know that my code is pretty messy, as I said, I have not been coding for 9 months, nothing excuses my absence :slight_smile:

I am writing in 2.7.11 python, it is like my first program since december, woke up today and wanted to write something.


A = [".-", "A"]
B = ["-...", "B"]
C = ["-.-.", "C"]
D = ["-..", "D"]
E = [".", "E"]
F = ["..-.", "F"]
G = ["--.", "G"]
H = ["....", "H"]
I = ["..", "I"]
J = [".---", "J"]
K = ["-.-", "K"]
L = [".-..", "L"]
M = ["--", "M"]
N = ["-.", "N"]
O = ["---", "O"]
P = [".--.", "P"]
Q = ["--.-", "Q"]
R = [".-.", "R"]
S = ["...", "S"]
T = ["-", "T"]
U = ["..-", "U"]
V = ["...-", "V"]
W = [".--", "W"]
X = ["-..-", "X"]
Y = ["-.--", "Y"]
Z = ["--..", "Z"]
list0 = [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]


def moorse():
    text = raw_input("Everything that you will type here will be translated to Morse Code. ")
    text.append(list0)
    for y in list0:
        return x


Morse code to text, stuck
#2

Hi, @granatnik ,

Consider what types of data structures would be best for translating from letters to International Morse Code.

You should review portions of the Codecademy Python track, especially Functions and Python Lists and Dictionaries. They present some of the concepts you can use for this project.


#3

Hi, thank you for reply.

Honestly I already was looking for help in the course a few times, I finished it long time ago so I could forgot something but haven't seen anything useful in there, so either I am blind or idk. Actually right now the code is working but I want it to translate like full words to morse and I don't really know how to make it happen. Also it is printing both values for some reason - normal letter and the morse version.


#4

This would be a good time to post the most recent version of your code, if you have made changes since your original post.


#5

Sorry, I wrote this sentence a bit wrong - the code that I posted is working - if I type any capital letter then it prints out the morse version and letter. But the thing is I want it to print out only morse version and make it print out morse version even if there is more than one letter.


#6

I would suggest dividing the task into components.

Create a dictionary in which each letter is a key and its International Morse Code representation is the value associated with that key.

Write a function that takes a string as a parameter and uses the dictionary to produce and return the International Morse Code representation of that string.


#7

Alright, I'll try later to learn anything about it and write it in the way you described, thanks for help :slight_smile:


#8

Hi, @granatnik .

I couldn't restrain myself from playing with this a bit :blush:, and here's my code, written very quickly, and perhaps not very cleanly. Feel free to use, modify, and clean it up, if you want.

The dictionary was created programmatically from your code, in order to save time, and that explains the strange order in which the items are listed. It looks funny, but it works.

Have fun!

class Morse(object):
    morse_dict = {'E': '.', 'U': '..-', 'T': '-', 'X': '-..-', 'Q': '--.-', 'W': '.--', 'R': '.-.', 'O': '---', 'I': '..', 'H': '....', 'F': '..-.', 'V': '...-', 'B': '-...', 'L': '.-..', 'G': '--.', 'P': '.--.', 'Y': '-.--', 'J': '.---', 'D': '-..', 'A': '.-', 'S': '...', 'K': '-.-', 'C': '-.-.', 'M': '--', 'Z': '--..', 'N': '-.'}
    def trans(self, letters):
        out = []
        for ch in letters:
            if ch.upper() in Morse.morse_dict:
                out.append(Morse.morse_dict[ch.upper()])
            else:
                out.append(" ")
        return " ".join(out)
    def get_input(self):
        mess = raw_input("Enter your message: ")
        return mess
    def interact_with_user(self):
        while True:
            mess = self.get_input()
            if len(mess) > 0:
                print(self.trans(mess))
            else:
                break

morse = Morse()
morse.interact_with_user()

#9

Oh wow, thanks, I will try to make something similar that will work :D. I am surprised you actually wrote it for me, thanks appylpye :).


#10

... was just building upon the program that you started; now you can improve upon what I did, and post the result. :wink:


#11

class Morse(object):
    morse_d = {'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': '-----', '.': '.-.-.-', ',': '--..--', "'": '.----.', '"': '.-..-.',
               '_': '..--.-', ':': '---...', ';': '-.-.-.', '?': '..--..', '!': '-.-.--',
               '-': '-....-', '+': '.-.-.', '/': '-..-.', '(': '-.--.', ')': '-.--.-',
               '=': '-...-', '@': '.--.-.', ' ': ' '}
    
    def translate(self, message):
        output = str()
        for letter in message:
            if letter.upper() in self.morse_d:
                output += self.morse_d[letter.upper()]
            else:
                print('There is no symbol like that!')
        return output
    
    def get_input(self):
        msg = raw_input("Everything that you will type here will be translated to Morse Code. ")
        return msg
    
    def interaction(self):
        while True:
            msg = self.get_input()
            if len(msg) > 0:
                print(self.translate(msg))
            else:
                break

morse = Morse()
morse.interaction()

Here is a little bit modified code, it works pretty well I must say, big thanks to you appylpye for helping me with it, starting to learn how to code is not that easy I must say and you made it in this case a lot easier for me to understand. Big thanks to you.


#12

Nice work, @granatnik :slight_smile:

    -. .. -.-. .   .-- --- .-. -.-   --. .-. .- -. .- - -. .. -.-

#13

Well, now you gave me idea to make it translate both ways - morse to letters and letters to morse, maybe I'll make it happen later, thanks :slight_smile:


#14

Great idea, @granatnik; ... looking forward to seeing that. :slight_smile:


#15

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.