Reputation: 13
I have to create a program that asks the user for an input file, and then creates an output file containing the message encoded in Morse code. When I run my program, there's a type error in the "translated += alphabet[words]" line, it says it's an unhashable type: 'list'. How can I translate the text in the input file to morse code after opening it?
Is the problem with my code after the function?
inputFileName = input("Enter the input file name:")
outputFileName = input("Enter the output file name:")
def morseCode(inputFileName):
inputFileName = inputFileName.upper()
translated = ""
# Open the input and output files
with open(inputFileName) as inputFile, open (outputFileName, "w") as outputFile:
for line in inputFile:
words = line.split()
# Translate letters in dictionary
translated += alphabet[line]
for word in words:
if word in inputFileName:
outputFile.write(inputFile[word])
else:
outputFile.write(word)
outputFile.write(' ')
outputFile.write('\n')
return (outputFile, inputFile, inputFileName, translated)
translated = morseCode(inputFileName)
print(translated)
Upvotes: 0
Views: 1416
Reputation: 182
MORSE_CODE_DICT = { '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':'--..',
'1':'.----', '2':'..---', '3':'...--',
'4':'....-', '5':'.....', '6':'-....',
'7':'--...', '8':'---..', '9':'----.',
'0':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-'}
def encrypt(message):
cipher = ''
message_upper=message.upper()
for letter in message_upper:
if letter != ' ':
if letter in MORSE_CODE_DICT:
cipher += MORSE_CODE_DICT[letter] + ' '
else:
cipher+=letter
else:
cipher += ' '
return cipher
O/P:-
>>> encrypt('I like apples + bananas!')
'.. .-.. .. -.- . .- .--. .--. .-.. . ... + -... .- -. .- -. .- ... !'
Upvotes: 1
Reputation: 4544
Once you have a dict of characters to morse, you can use a simple list comprehension and the dict.get()
feature to translate to Morse. Note, this converts any non alpha-numeric characters to a space. As you can see in the other answer, you can easily add those characters to your dict. One key you need to pay attention to is the str.upper()
method. This will make sure that all the characters have a match since Morse is case insensitive.
def txt_2_morse(msg):
morse = {
'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':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-',
'5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.',
'0':'-----'}
return "".join([morse.get(c.upper(), ' ') for c in msg])
print(txt_2_morse("Hello World"))
# ......-...-..--- .-----.-..-..-..
So, if you want to read a file in line by line into an output file, you can simply parse each line individually.
with open('inputfile.txt') as infile:
with open('outputfile.txt', 'w') as outfile:
for line in infile:
outfile.write("{}\n".format(txt_2_morse(line)))
Input:
this is a file
with a few lines in it
to demonstrate code
Output:
-......... ..... .- ..-....-...
.--..-.... .- ..-...-- .-....-..... ..-. ..-
---- -...------....-.-..--. -.-.---.
Upvotes: 0