小编dan*_*dan的帖子

如何在python中一次读取一个字母的字符串

我需要将用户输入的字符串转换为莫尔斯电码.我们教授希望我们这样做的方法是从morseCode.txt文件中读取,将morseCode中的字母分成两个列表,然后将每个字母转换为morse代码(当有空格时插入一个新行).

我有个开始.它的作用是读取morseCode.txt文件并将字母分成列表[A,B,... Z]并将代码分成列表[' - - .. - - \n','. - . - . - \n" ...].

我们还没有学过"套装",所以我不能用它.然后,我如何获取他们输入的字符串,逐字逐句,并将其转换为莫尔斯电码?我有点陷入困境.这就是我现在所拥有的(根本不是......)

编辑:完成程序!

# open morseCode.txt file to read
morseCodeFile = open('morseCode.txt', 'r') # format is <letter>:<morse code translation><\n>   
# create an empty list for letters
letterList = []    
# create an empty list for morse codes
codeList = []
# read the first line of the morseCode.txt
line = morseCodeFile.readline()    
# while the line is not empty
while line != '':        
    # strip the …
Run Code Online (Sandbox Code Playgroud)

python

11
推荐指数
2
解决办法
10万
查看次数

标签 统计

python ×1