#๐ my morse translator get an error
15 messages ยท Page 1 of 1 (latest)
@raw halo
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
It is because words is not defined
You need to change the variable word to be words
This is the correct code:
type_here = input('>')
words = type_here.split()
morse_code = {
'a'.upper(): "._",
'b'.upper(): "_...",
'c'.upper(): "_._.",
'd'.upper(): "_..",
'e'.upper(): ".",
'f'.upper(): ".._.",
'g'.upper(): "__.",
'h'.upper(): "....",
'i'.upper(): "..",
'j'.upper(): ".___",
'k'.upper(): "_._",
'l'.upper(): "._..",
'm'.upper(): "__",
'n'.upper(): "_.",
'o'.upper(): "___",
'p'.upper(): ".__.",
'q'.upper(): "__._",
'r'.upper(): "._.",
's'.upper(): "_",
't'.upper(): "...",
'u'.upper(): ".._",
'v'.upper(): "..._",
'w'.upper(): ".__",
'x'.upper(): "_.._",
'y'.upper(): "_.__",
'z'.upper(): "__..",
' ': '/'
}
output = " "
for word in words:
output += morse_code.get(word, word) + ' '
print(output)
type_here = input('> ')
words = type_here.split()
morse_code = {
'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': "__..",
' ': '/'
}
output = []
for word in words:
for letter in word.upper():
output.append(morse_code.get(letter, letter))
output.append('/') # Separate words with '/'
# Join the Morse code output with spaces and strip any trailing '/'
output_text = ' '.join(output).strip(' /')
print(output_text)
Not sure but the code Bashmega gave didn't work (at least for me, not sure if somehow it works on his machine) but;
This also allows multiple words and cycles through each letter nicely, It then translates it to morse code and puts it back together, I added a separation after each word so that might come in handy when trying to decode it
I think you're trying to search up whole words in that dictionary instead of letters, thus it reverts back to what input it got
I just fixed the error
Oh, in that code too?
The error was that words was undefined
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.