2017-03-14 10 views
-1

以下は私のコードです。 "for digit in observed"ループのどこかに、私が見つけたり修正したりできないようなインデックスエラーがあります。私はそれがPINSのリストとは関係があると信じていますが、私の編集のどれもが変わっていないので、わかりません。失敗したテストケースが観測された= '11'。単一のケースはすべて合格です。私はcodewarsを使用しています残念ながら、エラーのために与えられた何行は、存在しないだけで、次の文字列インデックスが範囲外でエラーを修正できません

トレースバック:範囲

のうち文字列インデックス:get_pins

はIndexErrorで

def get_pins(observed): 

    # Let's see what we're working with 
    print("observed") 
    print(observed) 
    print(" ") 

    # Dictionary of possible numbers for a half-assed observation of a key press. 
    possible = {'0':'08','1':'124','2':'1235','3':'236', 
       '4':'1457','5':'24568','6':'3569', 
       '7':'478','8':'05789','9':'689'} 

    # Single digit pwd case 
    PINS=[] 

    if len(observed) == 1: 
     for digit in possible[observed]: 
      PINS.append(digit) 
     return PINS 

    # Find number of possible PINs 
    num_possibles = 1 
    # Step through observed digits 
    for digit in observed: 
     num_possibles*=len(possible[digit]) 

    # Populate PINS to allow string manipulation 
    PINS = " "*num_possibles 
    print(PINS[num_possibles]) 
    num_change = num_possibles 
    change = [] 
    count = 0 

    # Step through observed, determine change of digit, 
    for digit in observed: 

     # Last digit in observed means it iterates every time 
     if digit != observed[len(observed)-1]: 

      # Develop array for checking position 
      num_change = num_change/len(possible[digit]) 

      for i in range(1,len(possible[digit])): 
       change.append(i*num_change) 

      print(change) 

      # Populate PINS with possible digit, full pin is created after final iteration of digit/observed loop 
      for pin in range(0,num_possibles-1): 
       PINS[pin] = PINS[pin] + possible[digit][count] 
       if (pin+1) in change: 
       count+=1  
      change=[] 
      count =0 
     else: 
      for pin in range(0,num_possibles-1): 
       PINS[pin] = PINS[pin] + possible[digit][count] 
       count+=1 
       if count == len(possible[digit]): 
        count = 0 

    return PINS 
+0

もっと情報をお願いします。どの行がエラーをスローしますか?実際のエラーメッセージは何ですか?また、問題を示す「観察された」サンプル入力を提供することで、これを[mcve]に変えると助けになります。 –

+0

編集を追加しました@JohnColeman – aeroterp3767

+0

'observation'入力のサンプルを意図した出力と一緒に与えると、それでも役立ちます。 –

答えて

0

問題はここにある:

PINS = " "*num_possibles 
print(PINS[num_possibles]) 

最初の行は、長さがnum_possiblesのスペース文字列を作成します。つまり、有効なインデックスは0, 1, ..., num_possibles - 1です。しかし、次の行では、存在しないインデックスnum_possiblesで文字列をインデックスしようとします。

私は単にそれを落とすだろうprint。それのポイントは何ですか? PINSはすべてのスペースの文字列なので、どうして気になるのですか?

文字列はPINSが文字列であることから、ライン

PINS[pin] = PINS[pin] + possible[digit][count] 

は、エラーが表示されます、不変である:あなたが何をすべき

TypeError: 'str' object does not support item assignment 

PINS

PINS = [' ']*num_possibles 

ように初期化することです

またはおそらくさらに良い

私はあなたがしているので、これはあなたが本当にやりたいことであるのかはわからないが、 - (それは +=を使用することにより短縮することができるが)

PINS[pin] = PINS[pin] + possible[digit][count] 

が合法になり、その場合には

PINS = ['']*num_possibles 

possibleに値として格納されている文字列を連結し、それらの文字列で表される数値を加算しないでください。

あなたのバグをいくつか修正する機能の終わりに

return PINSreturnによって「」.join(PINS)を交換 `

が、私が意図した入力でも意図した出力のいずれを知っているので、私が言うことができませんこれ以上。

関連する問題