I have created a translator for Morse code in Python and the code works fine except for one problem. When it translates the message into Morse code, it places each character onto a new line. I need the entire translation to stay in one line and I cannot figure out how to make it stay in one line, nor why it is generating a new line each time for each character. Please help! Here is the code:
print("\n\tMenu Options: \n")
print("\t1. Type a phrase and have it translated into Morse Code. \n\t2. Exit program \n")
# Define the main function.
def main():
charactersList = ['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','.',',','?','!',' ']
morse = ['.- ','-... ','-.-. ','-.. ','. ','..-. ','--. ','.... ','.. ','.--- ','-.- ','.-.. ','-- ','-. ','--- ','.--. ','--.- ',
'.-. ','... ','- ','..- ','...- ','.-- ','-..- ','-.-- ','--.. ','----- ','.---- ','..--- ','...-- ','....- ','..... ',
'-.... ','--... ','---.. ','----. ','.-.-.- ','--..-- ','..--.. ','-.-.-- ','/ ']
print("\nPlease select a menu option by typing 1 or 2.")
choice = int(input("\nMenu Choice: "))
# Start the while loop within the main function.
while choice != 2:
if choice == 1:
print("\nPlease type the phrase you would like translated into Morse Code.")
translatee=input()
translatee=translatee.lower()
for x in translatee:
if x in charactersList:
print("Translation: ",morse[charactersList.index(x)])
else:
print("Error: Please select a valid menu option.")
# See what the users' next choices are.
choice = int(input("\nMenu Choice: "))
print("\n\tExited.")
# Call the main function.
main()
input(exit)
Here is an example of the output: screenshot of output