Question
Python 3 Hello, i hope that you can help me implement my main python function here...im looking to import the dictionary and translator (last two
Python 3
Hello, i hope that you can help me implement my main python function here...im looking to import the dictionary and translator (last two .py attached "mydictionary and my translator)....can you please help me debug, well rather get all this to run as im having issues importing.....thank you....please feel free to change what you like...i simply need to have it run
class Application():
"""
console interface encapsulate Class
"""
def __init__(self):
self.input_str = ""
self.translated_str = ""
def Hello(self):
"""
Some Formatting
"""
print ("")
print ("============================")
print (" String To Morse Converter ")
print ("============================")
print ("")
def inputPrompt(self):
"""
Asks and takes input from the translator
"""
print ("Kindly enter a string you'd like convert into morse.")
print ("Valid characters include a-z, A-Z, 0-9, and \" \"")
print ("")
self.input_str = raw_input("input: ")
def translateInputToMorse(self):
"""
Provide string to translator.
If translator gives an error, dispay error message to console and exit.
"""
print (" ----- ")
print ("You gave: \"%s\"" % self.input_str)
MyTranslator = MyTranslator()
try:
upper_str = self.input_str.upper()
self.translated_str = MyTranslator.to_morse(upper_str)
except TranslationException as e:
print ("Error: %s" % e.message)
print ("\tIn input: %s" % e.expression)
print ("\tAt character: %s" % e.Element)
print ("")
print ("Please try again ")
exit(0)
def output_translation(self):
"""
Show user the translated text
"""
print ("Translation: %s" % self.translated_str)
def run():
Application = Application()
Application.Hello()
Application.inputPrompt()
Application.translateInputToMorse()
Application.output_translation()
if __name__ == "__main__":
run()
#MyDictionary.py
class MyDictionary():
def __init__(self):
self.myDictionary = {
"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": "_ _ . .",
"0": "_ _ _ _ _",
"1": ". _ _ _ _",
"2": ". . _ _ _",
"3": ". . . _ _",
"4": ". . . . _",
"5": ". . . . .",
"6": "_ . . . .",
"7": "_ _ . . .",
"8": "_ _ _ . .",
"9": "_ _ _ _ .",
" ": " " # Space remains space
}
#MyTranslator.py
class TranslationException(Exception):
"""
Exception occurs for bugs during conversion.
Attributes:
expression string input where error is found
Element Element that cannot be matched
message-- message displayed accordingly
"""
def __init__(self, expression, Element, msg):
self.expression = expression
self.Element = Element
self.message= message
from myDictionary import MyDictionary
class MyTranslator():
""" Morse Transaction Concept Encapsulation """
def __init__(self):
self.myDictionary = MyDictionary()
def to_morse(self, inputString):
characterList= []
for letter in inputString:
Element = self.myDictionary.find(letter)
if Element:
char_list.append(Element)
else:
raise TranslationException(inputString, letter, "Letter not found.")
return " ".join(characterList)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started