2016-11-05 8 views
0

だから、私はそれがユーザーによって入力されたテキストを取って辞書を作成することになっているいくつかのコードです。私はなぜmain()関数を呼び出すときにトレースバックエラーが発生するのか教えてください。main()を呼び出すときにエラーが発生するのはなぜですか?

def build_index(text): 
    index = {} 
    words = text.split() 

    position = 0 

    for x in text: 
     if x.isalpha() == False and x.isdigit() == False: 
      text.join(x) 
     else: 
      text.replace(x,'') 

    while position < len(words): 
     nextword = words[position] 

     if nextword in index: 
      ref = index[nextword] 
      ref.append(position) 
      index[nextword] = ref 

     else: 
      list = [] 
      list.append(position) 
      index[nextword] = list 
     position += 1 


def displayindex(index): 
    keys = sorted(index.keys()) 
    for key in keys: 
     print(key + ':' + str(index[key])) 

def main(): 
    text = input("enter text") 
    build_index(text) 
    displayindex(index) 
main() 
+5

トレースバックエラーを送信できますか? –

+2

Btwでは、 'text.join(x)'と 'text.replace(x、 '')'の戻り値を何にも割り当てていません。彼らは呼び出された文字列を変更しません。あなたが要求した変更で新しい文字列を返します。 –

+0

あなたの質問に_complete_ tracebackを含めるように質問してください。それは同じ問題に遭遇した他の人にあなたの疑問を助けるでしょう。また、他の人がこの質問を見つけるのを助けるために、(NameErrorのように)発生した例外の名前を含むタイトルを編集した場合にも役立ちます。 –

答えて

0

トレースバックエラー内容はあなたがあなたのコードを実行しているのPythonのバージョンに依存したPython 3.xのでは、それはエラーを生成だ、なぜ、トレースバックは説明する:

Traceback (most recent call last):
File "./prog.py", line 37, in
File "./prog.py", line 36, in main
NameError: name 'index' is not defined

TLDR:コードを3行追加/変更する必要があります。それはmain法の適用範囲外だと、まだ定義されて頂いていないので、それは、名前indexが参照するのか分からないことNameErrorが私たちに語っている

次のコード内のコメントを参照してください。あなたはMeterLongCatの答えで述べたようにindex変数のglobalインスタンスを作成することもできますが、私たちはbuild_indexを呼び出すときに、インデックス以来が作成され、定義されますない、私達はちょうど通過し、その後、そのメソッドを呼び出した後indexを返し、その戻り値を保存することができますそれはdisplayindex関数に次のようにします。

OTOHのPython 2では、MeterLongCatが指摘しているように、inputの文字列をユーザから取得したいと思っています。raw_inputが必要です。

def build_index(text): 
    index = {} 
    words = text.split() 

    position = 0 

    for x in text: 
     if x.isalpha() == False and x.isdigit() == False: 
      text.join(x) 
     else: 
      text.replace(x,'') 

    while position < len(words): 
     nextword = words[position] 

     if nextword in index: 
      ref = index[nextword] 
      ref.append(position) 
      index[nextword] = ref 

     else: 
      list = [] 
      list.append(position) 
      index[nextword] = list 
     position += 1 
    return index     # Return the index 


def displayindex(index): 
    keys = sorted(index.keys()) 
    for key in keys: 
     print(key + ':' + str(index[key])) 

def main(): 
    text = raw_input("enter text") # Use raw_input 
    index = build_index(text)  # Assign the index 
    displayindex(index) 
main() 
0

私は(Pythonの2.7用)raw_inputinputを変更することにより、トレースバックエラーを取り除くことができました。ただし、他のエラーがあります。例えば、mainindexが定義されていません。次のような作品:。

index = {} 

def build_index(text): 
    global index 
    words = text.split() 

    position = 0 

    for x in text: 
     if x.isalpha() == False and x.isdigit() == False: 
      text.join(x) 
     else: 
      text.replace(x,'') 

    while position < len(words): 
     nextword = words[position] 

     if nextword in index: 
      ref = index[nextword] 
      ref.append(position) 
      index[nextword] = ref 

     else: 
      list = [] 
      list.append(position) 
      index[nextword] = list 
     position += 1 


def displayindex(index): 
    keys = sorted(index.keys()) 
    for key in keys: 
     print(key + ':' + str(index[key])) 

def main(): 
    global index 
    text = raw_input("enter text") 
    build_index(text) 
    displayindex(index) 
main() 
+0

私はpythonの3.(?)バージョンを使用しています。申し訳ありませんが、私は指定しませんでした。 –

関連する問題