2016-12-16 4 views
-1
for x in non_neutral.collect(): 
tweet = str(x[2]) 
sid = x[1] 
status = x[0] 
text = word_tokenize(tweet) 
text1 = list(text) 
tweet = x[2].split() 
pronoun = intersect(second_pronoun,tweet) 
perojective = intersect(less_offensive,tweet) 
if pronoun: 
    pronoun_index = tweet.index(pronoun[0]) 
    pero_index = tweet.index(perojective[0]) 
if pero_index <= pronoun_index+3: 
    status = 1 
    return Row(status=status,tid=sid,tweet = str(tweet)) 
else: 
    status = 0 
    return Row(status=status,tid=sid,tweet = str(tweet)) 

私は常にこのエラーを取得していますし、私は再びコードを書いてみましたが、それでも同じになった理由のPython:このコードの特定のスニペットの関数エラー外側戻し

File "<ipython-input-5-0484b7e6e4fa>", line 15 
return Row(status=status,tid=sid,tweet = str(tweet)) 
SyntaxError: 'return' outside function 

私は理解していませんエラー。

+2

あなたの問題は間違いなくインデントが原因です。あなたの実際の問題よりもここでは間違っていると思います。 –

+2

インデントに加えて、return文もありますが、関数定義はありません。あなたはdef funcname(入力)を持っていますか:? – Kelvin

+0

さて、関数の外に 'return'があります。混乱するのは何ですか? –

答えて

1

コードスニペットにキーワードdefが表示されません。これは、関数定義の開始を示します。スニペットは関数の本体から取り出されていますか?ここで

は、forループで戻りの動作サンプルです:

from random import shuffle 

def loop_return(): 
    values = [0,1] 
    shuffle(values) 
    for i in values: 
     if i == 0: 
      return 'Zero first.' 
     if i == 1: 
      return 'One first.' 
+0

はい!私はテストのためにこのスニペットを取った。しかしforループです。それでループでreturn文を持つのは正しいのですか? – nile

+0

return文の唯一の要件は、関数の本体内に存在しなければならないということです。あなたのコードに構文的に何か問題があり、Pythonインタプリタが関数定義の開始を認識していないと思われます。私は私の答えにforループ内のリターンのサンプルを追加しました。 – Apollo2020

3

あなたのプログラムが実際に機能が含まれていません。 return文は関数内に含まれていなければなりませんが、この場合は何も定義していません。だから、限り、あなたはそれが動作する機能でコードを置くよう

def Foo(): 
    #Here is where you put all of your code 
    #Since it is now in a function a value can be returned from it 
    if pronoun: 
     pronoun_index = tweet.index(pronoun[0]) 
     pero_index = tweet.index(perojective[0]) 
    if pero_index <= pronoun_index+3: 
     status = 1 
     return Row(status=status,tid=sid,tweet = str(tweet)) 
    else: 
     status = 0 
     return Row(status=status,tid=sid,tweet = str(tweet)) 

Foo() 

は、より多くの次のようなものを(これはこれは一例であり、あなたのすべてのコードが含まれていないことに注意してください)してください。 Pythonの基本関数定義の構文は次のとおりです。def Foo(Bar):ここで、Fooは関数の名前で、Barは必要な任意のパラメータで、それぞれカンマで区切ります。

1

実際には機能がないため、何も返されません。コードを手続きにすることで修正することができます。

関連する問題