以下は私のコードです。 "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
もっと情報をお願いします。どの行がエラーをスローしますか?実際のエラーメッセージは何ですか?また、問題を示す「観察された」サンプル入力を提供することで、これを[mcve]に変えると助けになります。 –
編集を追加しました@JohnColeman – aeroterp3767
'observation'入力のサンプルを意図した出力と一緒に与えると、それでも役立ちます。 –