How to read a string one letter at a time in python
- by dan
I need to convert a string inputed by a user into morse code. The way our professor wants us to do this is to read from a morseCode.txt file, seperate the letters from the morseCode into two lists, then convert each letter to morse code (inserting a new line when there is a space).
I have the beginning. What it does is reads the morseCode.txt file and seperates the letters into a list [A, B, ... Z] and the codes into a list ['– – . . – –\n', '. – . – . –\n'...].
We haven't learned "sets" yet, so I can't use that. How would I then take the string that they inputed, go through letter by letter, and convert it to morse code? I'm a bit caught up. Here's what I have right now (not much at all...)
morseCodeFile = open('morseCode.txt', 'r')
letterList = []
codeList = []
line = morseCodeFile.readline()
while line != '':
letterList.append(line[0])
codeList.append(line[2:])
line = morseCodeFile.readline()
morseCodeFile.close()
userInput = input("Enter a string to be converted to morse code or press <enter> to quit ")