莫尔斯电码转换器循环错误
编辑:现在完全修复。莫尔斯电码转换器循环错误
编辑:好,所以现在我把它改变'... --- ...'到'sos'(yippee!)但是由于某种原因,当我输入时它给了我一个KeyError'... --- .../... --- ...'。 这应该给我'sos sos',但我得到''的KeyError。 我认为这是因为在'/'之后,程序将其视为调用当前键的字典值的触发器。问题在于,由于前面的字符是'/',当时的当前键是空白的。我将如何解决这个问题? 谢谢。我很新的编程与蟒蛇(以及,一般编程,真的......),今天我有一个明智的想法,使莫尔斯电码转换器。
我有“纯文本莫尔斯电码”工作完美,但“莫尔斯电码纯文本”?
并非如此。
问题是,当我运行程序时,它不给我任何东西,它只是打破了它的循环(就像我告诉过的那样),没有任何东西回来给我。
如果你们能帮助我,我会非常感激。
哦,'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
现在,当我用'... --- ...'运行它时,它会回到开头,并且不会打印任何东西。 (通过开始,我的意思是我事先做好的菜单)
根据您的代码尝试是这样的: -
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用于替代if else
谢谢你,但是我按照你的建议运行了代码,现在我得到了''的KeyError。我认为这是因为如果文本=“... --- .../... --- ...”(其转换为sos sos),那么'/'之后的空间是一个问题,因为在这一点,它会尝试为字典调用一个空白键。你会如何解决这个问题? –
为了避免得到keyError,使用decode_dict.get(key,'') –
对不起,你能更详细吗? '/':''已经映射到字典中,并且仍然没有解决KeyError ... –
for循环仅考虑初始值text
。对循环中的text
所做的更改不起作用,因为text
是一个字符串,并且字符串是不可变的,所以您的修改不会修改要迭代的原始对象。结果是你的循环遍历所有原始的text
,完成循环而没有输入你的break
条件。
只需摆脱if text == ''
检查并将“打印结果”行移至for循环外。您不需要“删除使用过的文本”,因为当您到达想要执行该操作的时候,您已经遍历整个密钥。如果你只是选择离开的地方,那么无论如何你将开始在下一个循环迭代中解析下一个莫尔斯字母。
decoded_text + = decode_dict ['/'] 此行将生成一个错误。在decode_dict中没有键叫'/'。 – Sharad
假设空间' '
是在输入指定的隔板,可以使用这样的:
decoding_dict = {'...' : 's' , '---' : 'o'}
my_text = '... --- ...'
print(''.join(list(map(lambda x: decoding_dict.get(x, None),
输出:
sos
至于上面的代码中,有有几个问题:
- 修改“文本”,同时迭代它
- 检查文本=='',这从来没有发生。因此,结果从不打印。
试试这个:
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
@JustinSon:你可以在'if key ==''块中加入一个检查,检查这个键是否为空,如果是这样,就继续。 – BrenBarn
的'text'值永远不会空字符串。如果那是真的,你的循环已经结束了。除此之外,在循环内改变你正在迭代的东西的值通常是一个坏主意 –
@Burhan:虽然链接问题也是关于莫尔斯码,但它并不能解释代码的问题是什么在这个问题上。 – BrenBarn