2016-12-09 12 views
0

私はCodeWarsでPythonを練習することに決めましたので、ちょっと立ち往生して助けを求めるか、アドバイスをお願いします。一般的にモールスコードのデコードと 'NoneType'エラー(Python)

、これはモールスコードデコーダである、私はそれを自分で書いて、ここにコードがあります:私はVSのコードでそれを実行すると

def decodeMorse(morseCode): 
    letter, decodedMsg = '', '' 
    spaceCount = 0 

    morseABC = {'.-':'A', '-...':'B', '-.-.':'C', '-..':'D', 
        '.':'E', '..-.':'F', '--.':'G', '....':'H', 
        '..':'I', '.---':'J', '-.-':'K', '.-..':'L', 
        '--':'M', '-.':'N', '---':'O', '.--.':'P', 
        '--.-':'Q', '.-.':'R', '...':'S', '-':'T', 
        '..-':'U', '...-':'V', '.--':'W', '-..-':'X', 
        '-.--':'Y', '--..':'Z', '.----':'1', '..---':'2', 
        '...--':'3', '....-':'4', '.....':'5', '-....':'6', 
        '--...':'7', '---..':'8', '----.':'9', '-----':'0'} 
    for let in morseCode: #Gets each symbol from morseCode 
      if let != ' ': #Checks if the sym is not spacebar 
       letter += let #Adds sym to letter 
       spaceCount += 1 
      elif let == " " and spaceCount > 0: #Checks if the sym is spacebar 
       decodedMsg += morseABC.get(letter) #Adds the decoded letter 
       letter = '' #Flushes letter var 
       spaceCount = 0 
      else: 
       decodedMsg += ' ' 

    decodedMsg += morseABC.get(letter) #Writes the last word 
    decodedMsg = decodedMsg.replace(' ', ' ') #Replaces 2 spaces to 1 
    letter = ''   

    return decodedMsg 

decodeMorse('.... . .-.. .-.. --- .-- --- .-. .-.. -..') => HELLO WORLD 

、それは素晴らしい作品が、CodeWarsインタプリタは

があると言います
Traceback:  
    in  
    in decodeMorse 
TypeError: cannot concatenate 'str' and 'NoneType' objects 

多分あなたは私に何が間違っているのかを教えてくれたり、最適化を手伝ってくれたりします。私はスペースに困っていた。モールス文の3つのスペースは単語間のスペースです。

+0

@BPL、私は分かりません、私はちょうど "試行"ボタンを押して、それは6つのテストを行い、後にエラーについて通知します。ページのスクリーンショットを作成する必要がありますか? –

+0

代わりに、ここでもっと良いアプローチを試してみましょう。あなたはあなたの入力アルファベットが何かを知っていますよね? {'。'、 ' - '、 ''}エラーを再現するには、このアルファベットを使用してランダムな文字列を生成し、そのエラーを再現できるようになるまで機能を供給してください。そしてそれを簡単に修正することができます。もしあなたが望むなら、簡単な例 – BPL

+0

@BPLを投稿することもできます。 decodeMorse( '')とdecodeMorse( '')が原因です –

答えて

3

morseABC.get(letter)letterが終わりに誤ったスペースがあるときにエラーが発生しmorseABC

+0

リストに追加する必要がありますか? UPD:エラーを通知し続けました。 –

0

のキーに見つからない場合、実際Noneを返すことができます。

In [13]: decodeMorse('.... . .-.. .-.. --- .-- --- .-. .-.. -..') 
Out[13]: 'HELLO WORLD' 

In [14]: decodeMorse('.... . .-.. .-.. --- .-- --- .-. .-.. -.. ') 
--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-14-3ea2da5fb8ec> in <module>() 
----> 1 decodeMorse('.... . .-.. .-.. --- .-- --- .-. .-.. -.. ') 

<ipython-input-2-e2d6eb0e5152> in decodeMorse(morseCode) 
    23     decodedMsg += ' ' 
    24 
---> 25  decodedMsg += morseABC.get(letter) #Writes the last word 
    26  decodedMsg = decodedMsg.replace(' ', ' ') #Replaces 2 spaces to 1 
    27  letter = '' 

TypeError: Can't convert 'NoneType' object to str implicitly 

In [15]: 

getNoneのデフォルト値を返しますので。

私はそれは、単一のスペースに文字列splitへのより良いあなたの辞書に、単一の空間マッピングに空の文字列を追加し、その後、joinを使用するかもしれないと思う:

In [18]: message 
Out[18]: '.... . .-.. .-.. --- .-- --- .-. .-.. -.. ' 

In [19]: morseABC = {'.-':'A', '-...':'B', '-.-.':'C', '-..':'D', 
    ...:      '.':'E', '..-.':'F', '--.':'G', '....':'H', 
    ...:      '..':'I', '.---':'J', '-.-':'K', '.-..':'L', 
    ...:      '--':'M', '-.':'N', '---':'O', '.--.':'P', 
    ...:      '--.-':'Q', '.-.':'R', '...':'S', '-':'T', 
    ...:      '..-':'U', '...-':'V', '.--':'W', '-..-':'X', 
    ...:      '-.--':'Y', '--..':'Z', '.----':'1', '..---':'2', 
    ...:      '...--':'3', '....-':'4', '.....':'5', '-....':'6', 
    ...:      '--...':'7', '---..':'8', '----.':'9', '-----':'0', '':' '} 

In [20]: tokens = message.split(' ') 

In [21]: ''.join(morseABC[token] for token in tokens) 
Out[21]: 'HELLO WORLD ' 

In [22]: 
+0

私はちょっと分かりませんでした。 ''' .join(トークンのトークンとしてmorseABC [token])' 'の代わりに' 'decodingMsg + = morseABC.get ) '? –

+0

@ VadimKhいいえ、そのコードはfor-loopで始まるすべてのコードを置き換えます。上記はインタプリタシェルセッションです。 –

+0

私は理由を理解できませんが、コードを関数の下に置いたときにコードが機能しません。私はIDLEで行ごとに試してみましたが、機能は完璧でしたが機能しませんでした。 –

0

私は与えることはありません既に近いところにいるので、あなたのアルゴリズムのための修正プログラムです。代わりに、現在のバグのバージョンを修正するのに役立つ簡単なテクニックがあります。

import random 


def decodeMorse(morseCode): 
    letter, decodedMsg = '', '' 
    spaceCount = 0 

    morseABC = {'.-': 'A', '-...': 'B', '-.-.': 'C', '-..': 'D', 
       '.': 'E', '..-.': 'F', '--.': 'G', '....': 'H', 
        '..': 'I', '.---': 'J', '-.-': 'K', '.-..': 'L', 
        '--': 'M', '-.': 'N', '---': 'O', '.--.': 'P', 
        '--.-': 'Q', '.-.': 'R', '...': 'S', '-': 'T', 
        '..-': 'U', '...-': 'V', '.--': 'W', '-..-': 'X', 
        '-.--': 'Y', '--..': 'Z', '.----': '1', '..---': '2', 
        '...--': '3', '....-': '4', '.....': '5', '-....': '6', 
        '--...': '7', '---..': '8', '----.': '9', '-----': '0'} 
    for let in morseCode: # Gets each symbol from morseCode 
     if let != ' ': # Checks if the sym is not spacebar 
      letter += let # Adds sym to letter 
      spaceCount += 1 
     elif let == " " and spaceCount > 0: # Checks if the sym is spacebar 
      decodedMsg += morseABC.get(letter) # Adds the decoded letter 
      letter = '' # Flushes letter var 
      spaceCount = 0 
     else: 
      decodedMsg += ' ' 

    decodedMsg += morseABC.get(letter) # Writes the last word 
    decodedMsg = decodedMsg.replace(' ', ' ') # Replaces 2 spaces to 1 
    letter = '' 

    return decodedMsg 

random.seed(1) 

for i in range(10): 
    print('-' * 80) 
    test = ''.join(random.choice(['.', '-', ' ']) for _ in range(20)) 
    print(test) 
    try: 
     print(decodeMorse(test)) 
    except Exception as e: 
     print(e) 

は、出力は次のようになります:

-------------------------------------------------------------------------------- 
. .-.--- -..-.-- . - 
Can't convert 'NoneType' object to str implicitly 
-------------------------------------------------------------------------------- 
- . .-... .- .- . . 
Can't convert 'NoneType' object to str implicitly 
-------------------------------------------------------------------------------- 
-- .-. .--.- .. -. 
Can't convert 'NoneType' object to str implicitly 
-------------------------------------------------------------------------------- 
- - .-- - - .-. 
Can't convert 'NoneType' object to str implicitly 
-------------------------------------------------------------------------------- 
-- .- -.- .. --- 
MA K IO 
-------------------------------------------------------------------------------- 
.-.- - .. ... . 
Can't convert 'NoneType' object to str implicitly 
-------------------------------------------------------------------------------- 
- - --- .- . .- 
TTO A E A 
-------------------------------------------------------------------------------- 
.-- . -----. -- 
Can't convert 'NoneType' object to str implicitly 
-------------------------------------------------------------------------------- 
.. . .. -. ...-.-.- 
Can't convert 'NoneType' object to str implicitly 
-------------------------------------------------------------------------------- 
. .--...- . - -- -- 
Can't convert 'NoneType' object to str implicitly 

あなたが見ることができるようにすることができます。この技術により、あなたはcodewarsのウェブサイトのエラーを再現することができますまで、トリックは、ランダムな文字列を使用してアルゴリズムを供給していますこれらのタイプのアルゴリズムを簡単にテストできます。覚えて、アルゴリズムはどんな種類の入力でも動作するはずですので、良いケースでフィードするだけではなく、のすべてのタイプを入力してください。入力。ソリューションを直接提供するのではなく、このタイプの回答が良い場合もあります。それが役に立てば幸い。

+0

ありがとうございました! :D –

関連する問題