摩尔斯电码转换器循环错误

时间:2016-08-01 04:36:45

标签: python loops dictionary

编辑:现在完全修复。

编辑:好的,所以现在我让它改变'... --- ...'到'sos'(yippee!)但是出于某种原因,当我输入'...时,它给了我一个KeyError - ...... / ...... --- ...... 这应该给我'sos sos',但我得到''的KeyError。 我认为这是因为在'/'之后,有一个空间,程序将其视为调用当前键的字典值的触发器。问题是,当时的当前密钥是空白的,因为前一个字符是'/'。我怎么能解决这个问题? 感谢。

我很擅长使用p​​ython进行编程(好吧,编程一般,真的......)今天我有了制作摩尔斯电码转换器的好主意。

我的“纯文本到莫尔斯代码”工作完美无缺,但“莫尔斯代码到纯文本”?

没那么多。

问题在于,当我运行程序时,它不会给我任何东西,它只是打破它的循环(就像我告诉它的那样)而没有任何东西回复给我。

如果你们能帮助我,我会非常感激。

哦,'decode_dict'是我制作的字典,它将莫尔斯代码值与纯文本相关联。 例如,

decoding_dict = {'...' : 's' , '---' : 'o'}

依此类推。

def decode(text):
    text += ' ' 
    #I have it set up to trigger when there's a space, hence this.
    global key
    global decoded_text
    #I thought maybe this would fix it, it didn't. :(
    key = ''
    decoded_text = ''
    for i in text:
        if i == '.':
            key += i  #This adds a '.' to the key to reference later.
            continue
        elif i == '-':
            key += i   #See above comment.
            continue
        elif i == ' ':
            decoded_text += decoding_dict[key]
            text = text[(len(key) + 1) :]
            key = ''   #Calls the value of the key, cuts out the used text, and resets key.
            continue
        elif i == '/':
            decoded_text += decoding_dict['/']
            continue #In morse code, a '/' is a ' ' and that's in the dict.
        elif text == '':
            print "Result: " + decoded_text
            break #This is basically the end of the loop
        else:
            print "Error, please try again."
            break

现在当我用'... --- ...'运行它时,它会回到开头并且不会打印任何东西。 (一开始,我指的是我事先制作的菜单。)

3 个答案:

答案 0 :(得分:0)

根据你的代码尝试这样的事情: -

def decode(text):
    text += '$' 
    #I have it set up to trigger when there's a space, hence this.
    global key
    global decoded_text
    #I thought maybe this would fix it, it didn't. :(
    key = ''
    decoded_text = ''
    for i in text:
        if i == '.':
            key += i  #This adds a '.' to the key to reference later.
            continue
        elif i == '-':
            key += i   #See above comment.
            continue
        elif i == ' ':
            decoded_text += decoding_dict[key]
            text = text[(len(key) + 1) :]
            key = ''   #Calls the value of the key, cuts out the used text, and resets key.
            continue
        elif i == '/':
            decoded_text += decoding_dict['/']
            continue #In morse code, a '/' is a ' ' and that's in the dict.
        elif text == '$':
            print "Result: " + decoded_text
            break #This is basically the end of the loop
        else:
            print "Error, please try again."
            break

根据我的建议试试这个: -

def decode(text):
    error = False
    #I have it set up to trigger when there's a space, hence this.
    global key
    global decoded_text
    #I thought maybe this would fix it, it didn't. :(
    key = ''
    decoded_text = ''
    for i in text:
        if i == '.':
            key += i  #This adds a '.' to the key to reference later.
            continue
        elif i == '-':
            key += i   #See above comment.
            continue
        elif i == ' ':
            decoded_text += decoding_dict[key]
            text = text[(len(key) + 1) :]
            key = ''   #Calls the value of the key, cuts out the used text, and resets key.
            continue
        elif i == '/':
            decoded_text += decoding_dict['/']
            continue #In morse code, a '/' is a ' ' and that's in the dict.
        else:
            print "Error, please try again."
            error = True
            break
    else:
       If not error:
           print "Result: " + decoded_text

Last else用于for而不是if else

答案 1 :(得分:0)

for循环仅考虑text的初始值。您对循环中text的更改无效,因为text是一个字符串,字符串是不可变的,因此您的修改不会修改您重复的原始对象。结果是您的循环遍历所有原始text,完成循环而无需输入任何break条件。

只需摆脱if text == ''检查并移动"打印结果"在for循环之外的行。您不需要删除使用过的文本",因为当您达到想要这样做的时候,您已经迭代了整个密钥。如果您只是从上次停下的位置开始,那么无论如何,您将在下一次循环迭代中开始解析下一个莫尔斯字母。

答案 2 :(得分:0)

假设空格' '是您指定的输入中的分隔符,您可以使用:

decoding_dict = {'...' : 's' , '---' : 'o'}
my_text = '... --- ...'
print(''.join(list(map(lambda x: decoding_dict.get(x, None), 

输出:

sos

至于上面的代码,有几个问题:

  • 在迭代时修改'text'
  • 检查text ==''是否永远不会发生。因此,结果永远不会打印出来。

试试这个:

def decode(text):
    text += ' '
    #I have it set up to trigger when there's a space, hence this.
    global key
    global decoded_text
    #I thought maybe this would fix it, it didn't. :(
    key = ''
    decoded_text = ''
    for i in text:
        if i == '.':
            key += i  #This adds a '.' to the key to reference later.
            continue
        elif i == '-':
            key += i   #See above comment.
            continue
        elif i == ' ':
            decoded_text += decoding_dict[key]
            key = ''   #Calls the value of the key, cuts out the used text, and resets key.
            continue
        elif i == '/':
            decoded_text += decoding_dict['/']
            continue #In morse code, a '/' is a ' ' and that's in the dict.
        else:
            print("Error, please try again.")
            break
    print("Result: " + decoded_text)

致电:decode(my_text) 输出:

Result: sos