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 to_morse(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):
inp = input("Everything that you will type here will be translated to Morse Code. ")
return inp
def interaction(self):
while True:
inp = self.get_input()
if len(inp) > 0:
print(self.to_morse(inp))
else:
break
class Characters(object):
characters_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': '-----', '.': '.-.-.-' , ',': '--..--', "'": '.----.', '"':
'.-..-.', '_': '..--.-', ':': '---...', ';': '-.-.-.', '?': '..--..', '!': '-.-.--',
'-': '-....-', '+': '.-.-.', '-..-.': '/', '-.--.': '(', '-.--.-': ')', '-...-': '=',
'.--.-.': '@', ' ': ' '}
#for some reason when i added E(without '.' in characters_d) (same with '-' and 'T')
# to the characters_d it shows errors, probably because it only contains dot
#i do not really know how to fix it to be honest
#also i do not know how to make it work properly if '.' is as itself and as 'E' too, kinda hard for me
def from_morse(self, msg):
output_0 = str()
for symbols in msg:
if symbols in self.characters_d:
output_0 += self.characters_d #I do not really know how to make it replace the symbols ., - with letters
else:
print('There is no symbol like that!')
return output_0
#if this function and E/. + -/T will work then the rest will be alright, program will be working i guess
#i was writing it without access to the internet so it was kinda hard
#i only had my knowledge, which is very limited
def get_input0(self):
text = input("Everything that you will type here will be translated to alphabet from Morse Code. ")
return text
def interaction(self):
while True:
text = self.get_input0()
if len(text) > 0:
print(self.from_morse(text))
else:
break
def determine(self):
choice = input("What do you want? Translator from alphabet to Morse Code, or from Morse Code to alphabet?"
" Type 1 or 2. ")
if choice == "1":
morse = Morse()
morse.interaction()
elif choice == "2":
alphabet = Characters()
alphabet.interaction()
alphabet = Characters()
alphabet.determine()
#please do not write code for me, just try to make me understand and learn something, tell me what is wrong,
#and what i should look up, and then i will try my best to make it happen
#i think it can be written much shorter too but whatever
So basically I created a topic:
and I had some problems with it and I wanted to continue the topic because I wanted to add something new to the program but I am stuck now and I can’t find any solution on the internet so it’s kinda hard for me, I am a big newbie when it comes to programming, I only finished the basic course on codecademy, that’s all. Please don’t post like full code because I don’t think I will learn anything from it. Basically I want it to work in the morse-text way too, tried to do it without internet connection (because I haven’t had any in past 7 days, thats why the topic got closed, so i can’t write in it now :/) but I got stuck and didn’t have enough knowledge to know what to do. Any help/advice/comment will be greatly appreciated, i’ve already put some comments in the code so, that’s all, help a noob pls