Why not just iterate through the string?
a_string="abcd"
for letter in a_string:
print letter
returns
a
b
c
d
So, in pseudo-ish code, I would do this:
user_string = raw_input()
list_of_output = []
for letter in user_string:
list_of_output.append(morse_code_ify(letter))
output_string = "".join(list_of_output)
Note: the morse_code_ify function is pseudo-code.
You definitely want to make a list of the characters you want to output rather than just concatenating on them on the end of some string. As stated above, it's O(n^2): bad. Just append them onto a list, and then use "".join(the_list).
As a side note: why are you removing the spaces? Why not just have morse_code_ify(" ") return a "\n"?
Why not just iterate through the string?
a_string="abcd"
for letter in a_string:
print letter
returns
a
b
c
d
So, in pseudo-ish code, I would do this:
user_string = raw_input()
list_of_output = []
for letter in user_string:
list_of_output.append(morse_code_ify(letter))
output_string = "".join(list_of_output)
Note: the morse_code_ify function is pseudo-code.
You definitely want to make a list of the characters you want to output rather than just concatenating on them on the end of some string. As stated above, it's O(n^2): bad. Just append them onto a list, and then use "".join(the_list).
As a side note: why are you removing the spaces? Why not just have morse_code_ify(" ") return a "\n"?
A couple of things for ya:
The loading would be "better" like this:
with file('morsecodes.txt', 'rt') as f:
for line in f:
line = line.strip()
if len(line) > 0:
# do your stuff to parse the file
That way you don't need to close, and you don't need to manually load each line, etc., etc.
for letter in userInput:
if ValidateLetter(letter): # you need to define this
code = GetMorseCode(letter) # from my other answer
# do whatever you want
Videos
» pip install readchar