I want to write a program that asks the user for a message, then converts the message using the telephony codes, codes that translate each letter into a specific word. Here is sample output from the program:
This program will translate a message using telephony codes. What is your message? I love you, mom! India Lima Oscar Victor Echo Yankee Oscar Uniform Mike Oscar Mike
The solution I can think of is to replace the letter a to alfa and then b and then rest of the list but it is just time consuming; My question is: how can I use a for loop (maybe?) to set conditions and to convert all letters?
Basically you need to convert every letter into a new word using the dictionaries
"A": "Alfa", "B": "Bravo", "C": "Charlie", "D": "Delta", "E": "Echo", "F": "Foxtrot", "G": "Golf", "H": "Hotel", "I": "India", "J": "Juliett", "K": "Kilo", "L": "Lima", "M": "Mike", "N": "November", "O": "Oscar", "P": "Papa", "Q": "Quebec", "R": "Romeo", "S": "Sierra", "T": "Tango", "U": "Uniform", "V": "Victor", "W": "Whiskey", "X": "X-ray", "Y": "Yankee", "Z": "Zulu",
CodePudding user response:
output = ''
for letter in list(word):
if output == '':
output = dictionary[letter]
else:
output = output ' ' dictionary[letter]
I hope this helps. It checks if it is the first word added to the output, and then determines whether or not to add a space. word is the input, output is the result
CodePudding user response:
sample = "This program will translate a message using telephony codes. What is your message? I love you, mom! India Lima Oscar Victor Echo Yankee Oscar Uniform Mike Oscar Mike"
for letter, word in {"A": "Alfa", "B": "Bravo", "C": "Charlie", "D": "Delta", "E": "Echo", "F": "Foxtrot", "G": "Golf", "H": "Hotel", "I": "India", "J": "Juliett", "K": "Kilo", "L": "Lima", "M": "Mike", "N": "November", "O": "Oscar", "P": "Papa", "Q": "Quebec", "R": "Romeo", "S": "Sierra", "T": "Tango", "U": "Uniform", "V": "Victor", "W": "Whiskey", "X": "X-ray", "Y": "Yankee", "Z": "Zulu"}.items():
sample = sample.replace(word, letter)
sample
>>> 'This program will translate a message using telephony codes. What is your message? I love you, mom! I L O V E Y O U M O M'
CodePudding user response:
so first before parsing the string I would suggest removing the spaces and special characters like punctuations. Either remove the punctuations or add that to the lookup dictionary
lookup = {",": 'com',"A": "Alfa", "B": "Bravo", "C": "Charlie", "D": "Delta", "E": "Echo", "F": "Foxtrot", "G": "Golf", "H": "Hotel", "I": "India", "J": "Juliett", "K": "Kilo", "L": "Lima", "M": "Mike", "N": "November", "O": "Oscar", "P": "Papa", "Q": "Quebec", "R": "Romeo", "S": "Sierra", "T": "Tango", "U": "Uniform", "V": "Victor", "W": "Whiskey", "X": "X-ray", "Y": "Yankee", "Z": "Zulu"}
#here punctuations have been removed before hand
st = "This program will translate a message using telephony codes What is your message I love you mom India Lima Oscar Victor Echo Yankee Oscar Uniform Mike Oscar Mike".upper()
st = st.replace(' ', ',') #replacing spaces with comas
print(''.join(list(map(lambda x:lookup[x], st))))