所以下面是我的代碼。在「觀察數字」循環中的某處存在索引錯誤,我似乎無法找到或修復。我相信這與PINS列表有關,但我不確定,因爲我的編輯沒有任何改變。觀察失敗的測試用例='11'。單個案件全部通過。不幸的是,因爲我使用codewars,沒有任何的錯誤定行,只是執行以下操作:字符串索引超出範圍,我無法修復錯誤
回溯:
在
在get_pinsIndexError:字符串索引超出範圍
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
如果您提供了「已觀察」輸入樣本,以及預期輸出結果,它仍然有幫助。 –