2016-08-01 251 views
0

編輯:現在完全修復。莫爾斯電碼轉換器循環錯誤

編輯:好,所以現在我把它改變'... --- ...'到'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 

現在,當我用'... --- ...'運行它時,它會回到開頭,並且不會打印任何東西。 (通過開始,我的意思是我事先做好的菜單)

+1

的'text'值永遠不會空字符串。如果那是真的,你的循環已經結束了。除此之外,在循環內改變你正在迭代的東西的值通常是一個壞主意 –

+0

@Burhan:雖然鏈接問題也是關於莫爾斯碼,但它並不能解釋代碼的問題是什麼在這個問題上。 – BrenBarn

回答

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用於替代if else

+0

謝謝你,但是我按照你的建議運行了代碼,現在我得到了''的KeyError。我認爲這是因爲如果文本=「... --- .../... --- ...」(其轉換爲sos sos),那麼'/'之後的空間是一個問題,因爲在這一點,它會嘗試爲字典調用一個空白鍵。你會如何解決這個問題? –

+0

爲了避免得到keyError,使用decode_dict.get(key,'') –

+0

對不起,你能更詳細嗎? '/':''已經映射到字典中,並且仍然沒有解決KeyError ... –

0

for循環僅考慮初始值text。對循環中的text所做的更改不起作用,因爲text是一個字符串,並且字符串是不可變的,所以您的修改不會修改要迭代的原始對象。結果是你的循環遍歷所有原始的text,完成循環而沒有輸入你的break條件。

只需擺脫if text == ''檢查並將「打印結果」行移至for循環外。您不需要「刪除使用過的文本」,因爲當您到達想要執行該操作的時候,您已經遍歷整個密鑰。如果你只是選擇離開的地方,那麼無論如何你將開始在下一個循環迭代中解析下一個莫爾斯字母。

+0

decoded_text + = decode_dict ['/'] 此行將生成一個錯誤。在decode_dict中沒有鍵叫'/'。 – Sharad

0

假設空間' '是在輸入指定的隔板,可以使用這樣的:

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 
+0

@JustinSon:你可以在'if key ==''塊中加入一個檢查,檢查這個鍵是否爲空,如果是這樣,就繼續。 – BrenBarn