我想编写一个程序,向用户请求一条消息,然后使用电话代码将消息转换为电话代码,这些代码将每个字母翻译成一个特定的单词。下面是程序的示例输出:
这个程序将使用电话代码翻译消息。你想说什么?我爱您,妈妈!印度利马奥斯卡维克多回声洋基奥斯卡制服麦克奥斯卡麦克
我能想到的解决方案是将字母a替换为alpha,然后是b,然后是列表的其余部分,但这只是浪费时间;我的问题是:我如何使用for循环(也许?)来设置条件并转换所有字母?
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"我:"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",
output ="
for letter in list(word):
if output == '':
output = dictionary[letter]
else:
output = output + ' ' + dictionary[letter]
我希望这对你有帮助。它检查它是否是添加到输出中的第一个单词,然后确定是否添加空格。Word是输入,输出是结果
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'
所以在解析字符串之前,我建议删除空格和标点等特殊字符。要么删除标点符号,要么将其添加到查找字典
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))))