2017-05-26 5 views
0

文字列をリスト内の項目と照合する機能があり、リスト項目のインデックス番号を返しますマッチです。以下のように:TypeError:whileループで+: 'NoneType'および 'int'のサポートされていないオペランドタイプ

def get_int(get_wd, get_list): 
    for i, j in enumerate(get_list): 
     if j == get_wd: 
     get_i = i 
     return get_i 

main関数でwhileループは、機能上からの戻り整数を得ることがあります:

>> li_a = li_a[pos + 1:] 
>> TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' 

get_wd = [] 
x = 0 
candi = [] 

while len(li_a) > 0: 
    iter_a = iter(li_a) 
    srh_time = len(li_a) 
    while srh_time > 0: 
     temp = next(iter_a) 
     if temp in li_words: 
      candi.append(temp) 
     else: 
      pass 
     srh_time = srh_time - 1 
    max_len = max(len(s) for s in candi) 
    extr_wd = list(set(s for s in candi if len(s) == max_len)) 
    pos = get_int(extr_wd, li_a) ##Calling the function## 
    get_wd.append(extr_wd) 
    li_a = li_a[pos + 1:] 

私は、このエラーメッセージが出てい

私には何かアドバイスがありませんか?

+0

posは何に等しくない:

あなたはインデックス

を見つけるため.indexを使用することができますがget_intメソッドをリファクタリングtoは 'NoneType'です。また、元の関数に 'get_i'は必要ありません。 'j' == get_wd:' –

+0

私はあまりにも前にそれを試みたが、同じメッセージを持っている。 :( – htetmyet

答えて

2

私はget_intは、第二引数として最初listとしてstrまたはintを期待していると思いますが、pos = get_int(extr_wd, li_a)ここで両方の引数がlistあり、あなたがこの問題を解決する必要があります。あなたは上記に書いたあなたの関数は、したがって変数は、あなたがそれを割り当てる何かを返しませんでした、

def get_int(get_wd, get_list): 
    try: 
     return get_list.index(get_wd) 
    except ValueError: 
     return -1 # not found 
+0

ブーム!!、それは動作します。ありがとう:) – htetmyet

関連する問題