簡體   English   中英

Python循環,索引錯誤

[英]Python loop, Index error

我是 Python 新手,在第 6 行出現索引錯誤。這是怎么回事? 試圖創建一個簡單的遞歸模式來解決問題。

問題// 例如,假設數字列表是 [1, 3, 0, 1]。 0 重定向到 1,誰重定向到 3,誰重定向回 1。有兩個循環:1、3。因此答案是 2。請注意,即使您從 0 開始,它也不是循環的一部分.

我的函數必須輸出的一些測試用例

測試用例

輸入:(int list) numbers = [1, 0] 輸出:(int) 2

輸入:(int list) numbers = [1, 2, 1] 輸出:(int) 2

def answer(numbers,depth=0):
    if depth > 0:
        if depth > numbers:
            return false
        for m in range(0,depth):
            if numbers[m] == numbers[depth]:
                return depth+1;
    return answer(numbers, depth+1)
range(0,depth) 

返回 0,1,..,depth-1

if depth > numbers:

沒有意義:將整數與列表進行比較

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM