'How to check a specific alphabet in tuple first and move to the next alphabet in python?
i am trying to solve the morse code translator program. When i tried to encode the message the order of the morse code is not correct. I think it's because of the if statement as it displays the value what it finds first. Are there any alternatives for the if statement. I am specifically using the same function and need to access value from following tuple:
`MORSE_CODE = (
("-...", "B"), (".-", "A"), ("-.-.", "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"),("-.--.", "("), ("-.--.-", ")"),
(".-...", "&"), ("---...", ":"), ("-.-.-.", ";"),
("-...-", "="), (".-.-.", "+"), ("-....-", "-"),
("..--.-", "_"), (".-..-.", '"'), ("...-..-", "$"),
(".--.-.", "@"), ("..--..", "?"), ("-.-.--", "!")
)`
def encode(message):
while True:
words = [message]
words[:] = message
for i in range(0, len(MORSE_CODE)):
for j in range(0, len(words)):
if words[j] in MORSE_CODE[i][1]:
encoded_message = str(MORSE_CODE[i][0])
print(encoded_message, end=" ")
mode = str.upper(input("Would you like to encode/decode another message?y/n"))
if mode == 'Y':
encode(get_input())
break
elif mode == 'N':
print('Thanks for using the program :) GoodBye!!!')
break
return encoded_message
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

