Reputation: 55
In morse code: a space " " is written as a slash " / ". A gap between letters is written as a space " ".
I made a English-to-Morse translator. It was easy, since each letter was 1 character long, so I could separate it into characters. However, going the other way is hard. I need to be able to detect splits between letters, but also make sure I am not detecting splits between words.
Here is my python code:
print("\nNote: not all characters are logged. \n\n")
english = [ "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",".",",",";",":","!","?","(",")","-","_","!","&",
"=","+","$","/","'"," "]
morse = [ ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",
".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",
".--","-..-","-.--","--..",".----","..---","...--","....-",".....",
"-....","--...","---..","----.","-----",".-.-.-","-..-","-.-.-.",
"---...","--..--","..--..","-.--.","-.--.-","-....-","..--.-","--..--",
".-...","-...-",".-.-.","...-..-","-..-.",".----."," /"]
while True:
print ("English to Morse: press 1 ")
print ("Morse to English: press 2 ")
print ("What is morse code: press 3")
translate_direction = input("\n>").replace(" ","")
if translate_direction == "1":
tobetranslated = input("\nTranslate English to Morse Code\n\n> ").lower()
splitupinput = list(tobetranslated)
finishedoutput = ""
for i in splitupinput:
englishloc = english.index(i)
finishedoutput = finishedoutput + morse[englishloc]
finishedoutput = finishedoutput + " "
print(finishedoutput)
elif translate_direction == "2":
tobetranslated = input("\nTranslate Morse Code to English\n\n> ").lower()
tobetranslated = tobetranslated.replace(" ","ß ")
finishedoutput = ""
for i in splitupinput:
englishloc = english.index(i)
finishedoutput = finishedoutput + morse[englishloc]
finishedoutput = finishedoutput + " "
print(finishedoutput)
elif translated_irection == "3":
print("Morse code is... (insert long-winded explanation of the history of morse code and its applications)")
I am using .replace()
to replace the spaces with some character that is not in morse code plus the space, so that when I split along "π", which will be lost, I have a space left there so that I can determine splits between words.
But, I have an issue: I do not know how to split a string, along an undeterminable amount of splits, into a list.
Any help?
Fun fact: I only have the standard library. Sorry! I am a minor, and my parents aren't willing to download anything, no matter how much I ask.
I also am new to python, so if you could make a quick explanation as to how/why something works, that would be really nice :)
Upvotes: 2
Views: 248
Reputation: 33
You can use the string split function to split the string into a list of strings by a provided delimiter. For example, the following code splits a string by spaces into a string list.
stuff_to_split = "Hello there my friend"
list_of_strings = stuff_to_split.split(' ')
for word in list_of_strings:
print(word)
Outputs
Hello
there
my
friend
Upvotes: 0
Reputation: 108676
You need the built in .split()
method.
It does this sort of thing.
>>> '1/2/3'.split('/')
['1', '2', '3']
Read about it here. https://docs.python.org/3/library/stdtypes.html#string-methods
Upvotes: 3